为什么null是一个对象,null和undefined之间有什么区别? [英] Why is null an object and what's the difference between null and undefined?

查看:387
本文介绍了为什么null是一个对象,null和undefined之间有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么 null 在JavaScript中被视为对象

Why is null considered an object in JavaScript?

正在检查

if ( object == null )
      Do something

if ( !object )
      Do something

还有:

null undefined <之间的区别是什么? / code>?

What is the difference between null and undefined?

推荐答案

(name is undefined)

您:什么是名称(*)

JavaScript: name ?什么是名称?我不知道你在说什么。您以前从未提及任何名称。您是否在(客户端)方面看到了其他一些脚本语言?

You: What is name? (*)
JavaScript: name? What's a name? I don't know what you're talking about. You haven't ever mentioned any name before. Are you seeing some other scripting language on the (client-)side?

name = null;

您:什么是 name

JavaScript:我不知道。

简而言之; undefined 是没有东西概念的地方;它没有类型,并且在该范围之前从未被引用过; null 是已知存在的东西,但不知道该值是什么。

In short; undefined is where no notion of the thing exists; it has no type, and it's never been referenced before in that scope; null is where the thing is known to exist, but it's not known what the value is.

有一件事要做记住, null 在概念上与 false 等等,即使他们在类型转换后等同,即

One thing to remember is that null is not, conceptually, the same as false or "" or such, even if they equate after type casting, i.e.

name = false;

您:什么是 name

JavaScript:布尔值为false。

You: What is name?
JavaScript: Boolean false.

name = '';

您:什么是 name

JavaScript:空字符串

< sup> *: name 在此上下文中是指从未定义过的变量。它可以是任何未定义的变量。但是,name是几乎任何HTML表单元素的属性。它顺便说一下,并且在id之前很好地制定了。它很有用,因为id必须是唯一的,但名称不必是。

*: name in this context is meant as a variable which has never been defined. It could be be any undefined variable. However, name is a property of just about any HTML form element. It goes way, way back and was instituted well before id. It is useful because ids must be unique but names to not have to be.

这篇关于为什么null是一个对象,null和undefined之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆