=== 和 == 如何以不同方式处理 null 比较?

发布于 2024-12-15 07:32:07 字数 263 浏览 4 评论 0原文

我有一个简单的 json 解析对象,有时定义了变量 tt,有时则没有。

由于某种原因,jsonobject.tt == null 根据是否定义了 tt 正确返回 10。无论如何,jasonobject.tt === null 只是返回0。我认为 === 是用来避免问题的东西。

这是怎么回事?

I have a simple json parsed object that sometimes has a variable tt defined and sometimes doesn't.

For some reason jsonobject.tt == null returns correctly 1 or 0 based on whether tt is defined. jasonobject.tt === null just returns 0 regardless. I thought === was the thing to use to avoid issues.

What's going on here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

零崎曲识 2024-12-22 07:32:07

=== 是严格相等运算符,它比较类型和值。值 null 是 Null 类型,只有一个值 - null

Undefined 是未定义类型,它也只有一个值 - 'undefined'。

使用严格相等运算符时, null !== undefined 因为它们是不同的类型(请参阅严格相等比较算法的步骤 1,ECMA-262 § 11.9.6)。

== 是相等运算符。使用 == 的比较使用抽象相等比较算法 (ECMA-262 § 11.9.3),其中包括:

  1. 如果 Type(x) 与 Type(y) 相同,则...
  2. 如果 x 为 null 并且 y 未定义,则返回 true。
  3. 如果 x 未定义且 y 为 null,则返回 true。

因此,null == undefined 根据定义返回 true。严格来说,测试某个属性是否存在(无论其值是多少),应该使用 hasOwnProperty

if (jsonobject.hasOwnProperty('tt')) {
  // property exists
}

但实际上,与严格测试 undefined 没有太大区别:

if (jsonobject.tt === undefined) 

因为属性是否存在且值为未定义或者根本没有定义通常是等价的。使用 === 还意味着,如果 tt 存在但已被赋值为 null,则上述代码将返回 false。

=== is the strict equality operator, it compares type as well as value. The value null is the Null Type that has exactly one value - null.

Undefined is the Undefined Type, which also has only one value - 'undefined'.

When using the strict equality operator, null !== undefined because they are different types (see step 1 of the Strict Equality Comparison Algorithm, ECMA-262 § 11.9.6).

== is the equality operator. Comparisons using == use the Abstract Equality Comparison Algorithm (ECMA-262 § 11.9.3), which includes:

  1. If Type(x) is the same as Type(y), then ...
  2. If x is null and y is undefined, return true.
  3. If x is undefined and y is null, return true.

So null == undefined returns true by definition. Strictly, testing for the presence of a property (regardless of its value), should use hasOwnProperty:

if (jsonobject.hasOwnProperty('tt')) {
  // property exists
}

however in practice there isn't much difference to a strict test for undefined:

if (jsonobject.tt === undefined) 

because whether the property exists and has a value of undefined or hasn't been defined at all is usually equivalent. Using === also means that the above will return false if tt exists but has been assigned a value of null.

淡淡绿茶香 2024-12-22 07:32:07

由于某种原因(jsonobject.tt == null)正确返回1或0

首先比较返回 truefalse

其次你想要

jsonobject.tt === undefined

如果一个值不存在,那么它是未定义的。

其他检测方法有

!jsonobject.hasOwnProperty("tt");

!("tt" in jsonobject)

作为 == 是一个完全奇怪的运算符,null == undefined。因此,如果该属性不存在, jsonobject.tt == null 将返回 true

For some reason (jsonobject.tt == null) returns correctly 1 or 0

Firstly a comparison returns true or false

Secondly you want

jsonobject.tt === undefined

If a value does not exist it is undefined.

Other detection methods are

!jsonobject.hasOwnProperty("tt");

or

!("tt" in jsonobject)

As a side effect of == being a totally weird operator, null == undefined. Thus jsonobject.tt == null will return true if the property is not there

凹づ凸ル 2024-12-22 07:32:07

如果变量尚未定义,则其值为未定义

undefined == null

然而

undefined !== null

If a variable hasn't been defined it has a value of undefined.

undefined == null

whereas

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