当 JavaScript 返回 null 时不明确的?

发布于 2024-08-07 12:48:00 字数 803 浏览 3 评论 0原文

我使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

请爱~陌生人 2024-08-14 12:48:00

警报(山姆); // 返回未定义

不,这是一个例外。

当你访问一个未设置的属性时,你会得到undefined;直接使用未设置的名称时会出现错误。

全局变量很有趣,因为可以使用简单的变量名或使用 window 全局对象的属性来访问它们:

alert(window.sam);      // undefined
alert(window['sam']);   // undefined
alert('sam' in window); // false
alert(sam);             // ERROR

如果声明了 sam 但未初始化,则访问 < code>window.sam 仍然会导致 undefined,但原因不同:window 对象中有一个 sam 条目>,但它指向与访问不存在的属性时获得的相同的 undefined 对象。

var sam;
alert(window.sam);      // undefined
alert(window['sam']);   // undefined
alert('sam' in window); // ** true
alert(sam);             // ** undefined

这当然是一个令人困惑的血腥混乱; undefined 是 JavaScript 语言设计中最严重的错误之一。

另一方面,null 则很好,并且与其他语言中的 null/nil/void/None 值几乎相同。它不属于上述任何一项。

alert(sam); // returns undefined

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:

alert(window.sam);      // undefined
alert(window['sam']);   // undefined
alert('sam' in window); // false
alert(sam);             // ERROR

If sam is declared but not initialised, accessing window.sam still gets you undefined, but for a different reason: there is an entry in the window object for sam, but it points to the same undefined object as you get when you access a non-existant property.

var sam;
alert(window.sam);      // undefined
alert(window['sam']);   // undefined
alert('sam' in window); // ** true
alert(sam);             // ** undefined

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.

时光匆匆的小流年 2024-08-14 12:48:00
<script type="text/javascript">
// variable with an unasigned value
var a;
if (a == undefined) {
  alert('a is undefined');
}

if (a == null) {
  alert('a is undefined');
}

// this will produce an error
if (b == undefined) {
  alert('b is undefined');
}

// this is the right way to handle not defined variables
if (typeof(c) == 'undefined') {
  alert('c is blabla');
}
</script>
<script type="text/javascript">
// variable with an unasigned value
var a;
if (a == undefined) {
  alert('a is undefined');
}

if (a == null) {
  alert('a is undefined');
}

// this will produce an error
if (b == undefined) {
  alert('b is undefined');
}

// this is the right way to handle not defined variables
if (typeof(c) == 'undefined') {
  alert('c is blabla');
}
</script>
思慕 2024-08-14 12:48:00

对于要接收空值的变量,必须对其进行赋值。 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 from null.

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 in undefined is really saying "Are you sure you should be using this value it hasn't been assigned".

与往事干杯 2024-08-14 12:48:00

我区分它们的方式是 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."

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文