简体   繁体   English

在 JavaScript 中,使用 `a == null` 实际上等同于 `a === null || a === 未定义`?

[英]In JavaScript, is using `a == null` in fact the same as `a === null || a === undefined`?

It took me a while to note to always use === in JavaScript instead of == .我花了一段时间才注意到在 JavaScript 中总是使用===而不是==

But today I noticed但是今天我注意到

if (a == null) { ... }

seems identical as似乎与

if (a === null || a === undefined) { ... }

from a table in the MDN docs . 来自 MDN 文档中的表格

I hope to know:我希望知道:

  1. are they really identical?它们真的相同吗?
  2. is it recommended to use the form a == null instead of a === null || a === undefined是否建议使用形式a == null而不是a === null || a === undefined a === null || a === undefined ? a === null || a === undefined

Yes, they are identical, except for one case of document.all (please see the first comment).是的,它们是相同的,除了document.all一个案例(请参阅第一条评论)。 We can see that in the == chart, null and undefined compares to true only with itself and each other: null == null is true , and null == undefined is true , etc, and nothing else.我们可以看到,在==图表中, nullundefined仅在自身和彼此之间与true进行比较: null == nulltrue ,而null == undefinedtrue等等,没有别的。 So when we use something == null , the only something that can give a true is null and undefined :所以,当我们使用something == null ,唯一的something ,可以给一个truenullundefined

在此处输入图片说明

Even the TC39 is using it that way:甚至 TC39 也是这样使用它的:

https://github.com/tc39/proposal-optional-chaining/blob/master/README.md https://github.com/tc39/proposal-optional-chaining/blob/master/README.md

 a?.b // undefined if `a` is null/undefined, `ab` otherwise. a == null ? undefined : ab

However, I don't recommend using it in common daily coding.但是,我不建议在日常编码中使用它。 That's because, if the code a == null is read by other programmers, they may immediately change it to a === null and if the test cases are not able to catch it, then a subtle bug might be introduced in the system.这是因为,如果代码a == null被其他程序员读取,他们可能会立即将其更改为a === null ,如果测试用例无法捕获它,则可能会在系统中引入一个微妙的错误。

In an open source project where you have the ultimate decision for what goes in and what doesn't, then it can be used, with your code review and approval.在一个开源项目中,您对什么可以加入什么不可以有最终决定,然后可以使用它,并经过您的代码审查和批准。 Or, if you use it once or twice and then with proper comment up above to state what you are trying to do.或者,如果您使用它一次或两次,然后在上面加上适当的注释来说明您要做什么。

Otherwise, most other programmers may think if you really aim to compare just to null and not considering for undefined .否则,大多数其他程序员可能会认为您是否真的只想与null进行比较而不考虑undefined If you write a === null || a === undefined如果你写a === null || a === undefined a === null || a === undefined , then most everybody knows exactly what you are trying to do. a === null || a === undefined ,那么大多数人都知道你想要做什么。

A third side effect is, if you say a == null is identical to a === null || a === undefined第三个副作用是,如果你说a == null等同于a === null || a === undefined a === null || a === undefined , you could attract tons of users on Stack Overflow, downvoting on you, saying don't ever use == in JavaScript. a === null || a === undefined ,你可以在 Stack Overflow 上吸引大量用户,对你投反对票,说永远不要在 JavaScript 中使用==

An interesting fact is, even Babel translate a?.b to compare with both null and undefined ( void 0 ), instead of just using == null .一个有趣的事实是,即使 Babel 也将a?.b翻译a?.bnullundefined ( void 0 ) 进行比较,而不是仅使用== null Babel doesn't really have to, but maybe it does that so that when a programmer reads the compiled code, it is more clear. Babel 并不是真的必须这样做,但也许它这样做是为了让程序员在阅读编译后的代码时更清楚。

But I don't know how many interviewers have asked me if the line if (a == null) is good, and when I said we should use === , then I was viewed as just one step above greenhorn, because all the "pros" know that a == null is for checking whether a is null or undefined .但是我不知道有多少面试官问过我这行if (a == null)是否好,当我说我们应该使用=== ,我被认为只是比新手高出一步,因为所有的“专业人士”知道a == null用于检查anull还是undefined So while maybe it is not good to actually use it in the code, it is good for testing if somebody is a pro.因此,虽然在代码中实际使用它可能并不好,但它有利于测试某人是否是专业人士。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM