为什么 null 是一个对象,null 和 undefined 之间有什么区别? [英] Why is null an object and what's the difference between null and undefined?
问题描述
为什么 null
在 JavaScript 中被认为是 object
?
Why is null
considered an object
in JavaScript?
正在检查
if ( object == null )
Do something
与
if ( !object )
Do something
?
还有:
null
和 undefined
有什么区别?
推荐答案
(name is undefined)
你:什么是name
? (*)
JavaScript: name
?什么是名称
?我不知道你在说什么.您以前从未提到过任何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:布尔假.
name = '';
你:什么是name
?
JavaScript:空字符串
*: name
在此上下文中表示从未定义的变量.它可以是任何未定义的变量,但是,名称几乎是任何 HTML 表单元素的属性.它可以追溯到很久以前,并且在 id 之前就已经建立了.这很有用,因为 id 必须是唯一的,但名称不必是唯一的.
*: name
in this context is meant as a variable which has never been defined. It could 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 do not have to be.
这篇关于为什么 null 是一个对象,null 和 undefined 之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!