当 JavaScript 返回 null 时不明确的?
我使用 JavaScript 已经好几年了,从来不关心 null
和 null 之间的区别。 undefined
之前,我总是使用 undefined
来验证对象的存在。
但最近我读了这篇文章。他们在这里说
JavaScript 区分 null 和 undefined,前者是“object”类型的对象,表示有意为非值,后者是“undefined”类型的对象,表示未初始化的值,即值未初始化。甚至还没有被分配。我们稍后会讨论变量,但在 JavaScript 中,可以声明变量而不为其赋值。如果这样做,变量的类型将是未定义的。
我现在完全困惑了,这里到底什么是非价值。这个非值与 undefined
有何不同。什么情况下javascript会返回null
。
我已经尝试过下面的示例
var sam;
alert(sam); // returns undefined
并且
try {
//var sam;
alert(sam);
} catch(ex) { } // exception says: sam is undefined
我不确定 js 何时返回空值。有人可以帮我解释一下吗?
I have been using JavaScript for couple of years and never cared about the difference between null
& undefined
earlier, I always use undefined
to validate the object existence.
But recently I came through this article. Here they said
JavaScript distinguishes between null, which is an object of type 'object' that indicates a deliberate non-value, and undefined, which is an object of type 'undefined' that indicates an uninitialized value — that is, a value hasn't even been assigned yet. We'll talk about variables later, but in JavaScript it is possible to declare a variable without assigning a value to it. If you do this, the variable's type is undefined.
I am completely confused now, what exactly is non-value here. How this non-value differs from undefined
. And what are the circumstances javascript returns null
.
I have tried the below sample
var sam;
alert(sam); // returns undefined
And
try {
//var sam;
alert(sam);
} catch(ex) { } // exception says: sam is undefined
And I am not sure about when js returning nulls. Can someone clarify me.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
不,这是一个例外。
当你访问一个未设置的属性时,你会得到
undefined
;直接使用未设置的名称时会出现错误。全局变量很有趣,因为可以使用简单的变量名或使用
window
全局对象的属性来访问它们:如果声明了
sam
但未初始化,则访问 < code>window.sam 仍然会导致undefined
,但原因不同:window
对象中有一个sam
条目>,但它指向与访问不存在的属性时获得的相同的undefined
对象。这当然是一个令人困惑的血腥混乱;
undefined
是 JavaScript 语言设计中最严重的错误之一。另一方面,
null
则很好,并且与其他语言中的 null/nil/void/None 值几乎相同。它不属于上述任何一项。Nope, that's an exception.
You get
undefined
when you access an unset property; you get an error when you use an unset name directly.Global variables are interesting because they can be accessed either using a simple variable name, or by using properties of the
window
global object:If
sam
is declared but not initialised, accessingwindow.sam
still gets youundefined
, but for a different reason: there is an entry in thewindow
object forsam
, but it points to the sameundefined
object as you get when you access a non-existant property.This is of course a confusing bloody mess;
undefined
is one of the worst mistakes in the design of the JavaScript language.null
on the other hand is fine and works pretty much the same as null/nil/void/None values in other languages. It doesn't come into any of the above.对于要接收空值的变量,必须对其进行赋值。
null
用于指示未知或不关心的值。另一方面,undefined
旨在指示正在访问的属性从未被分配过值。这与null
不同。对于
null
,有人故意说“我还不知道这应该有什么价值”或“我不关心这现在是什么价值”。undefined
中的 OTH 实际上是在说“您确定应该使用这个尚未分配的值吗”。For a variable to receive a null value it must be assigned.
null
is used to indicate an unknown or don't care value.undefined
on the other hand is designed to indicate that the propery being accessed has never ben assigned a value. This differs fromnull
.With
null
one is deliberately saying "I don't know what value this should have yet" or "I don't care what value this is right now". OTH inundefined
is really saying "Are you sure you should be using this value it hasn't been assigned".我区分它们的方式是 undefined 是“我还没有定义这个值”,而 null 是“我已经定义了这个值,但我不知道或无法弄清楚该值应该是什么”。
The way I distinguish them is undefined being "I have not defined this value," and null being "I have defined this value, but I do not know or cannot figure out what the value should be."