We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
这个其实是一个很容易让人忽略的问题,我们知道一些在 JavaScript 中判断相等性的方法,但是对于一些特别的值,他们之间有一些微妙的差别。
另外,在 JavaScript 的内部实现里,到底是如何判断相等的,这是一件很有趣的事情。
这里 宽松相等的转型太多太杂,就不做对比了,主要比较 === 和 Object.is
===
Object.is
在这里对比了一些比较微妙的值,其实对于 === 和 Object.is 就只有这两个不同
Strict equality
Loose equality
Same-value equality
Same-value-zero equality
Array.prototype.indexOf
Array.prototype.lastIndexOf
%TypedArray%
ArrayBuffer
Map
Set
String.prototype.includes
Object.defineProperty
我认为,一般来说 === 在大多数场合中,都够用了。
只有在一些特殊情况下,我们需要判断 +0 和 -0 时,才需要使用手动使用 Object.is
另外,比较有趣的是 Map 和 Set 操作使用 Same-value-zero equality
JavaScript 中的相等性判断
The text was updated successfully, but these errors were encountered:
No branches or pull requests
这个其实是一个很容易让人忽略的问题,我们知道一些在 JavaScript 中判断相等性的方法,但是对于一些特别的值,他们之间有一些微妙的差别。
另外,在 JavaScript 的内部实现里,到底是如何判断相等的,这是一件很有趣的事情。
JavaScript 中的值的比较
这里 宽松相等的转型太多太杂,就不做对比了,主要比较
===
和Object.is
在这里对比了一些比较微妙的值,其实对于
===
和Object.is
就只有这两个不同Object.is
的 NaN 和 NaN 相等Object.is
的 +0 和 -0 不等JavaScript 中的相等算法
Strict equality
using ===Loose equality
using ==Same-value equality
using Object.isSame-value-zero equality
Same-value equality
区别在于,认为 +0 和 -0 相等这些算法用在哪里?
Strict equality
using ===Array.prototype.indexOf
Array.prototype.lastIndexOf
Loose equality
using ==Same-value-zero equality
%TypedArray%
和ArrayBuffer
构造函数Map
和Set
操作String.prototype.includes
Same-value equality
Object.defineProperty
在试图修改不可变属性时,如果这个属性确实被修改了则会抛出异常,反之什么都不会发生。这里的相等判断。实际应用
我认为,一般来说
===
在大多数场合中,都够用了。只有在一些特殊情况下,我们需要判断 +0 和 -0 时,才需要使用手动使用
Object.is
另外,比较有趣的是
Map
和Set
操作使用Same-value-zero equality
参考资料
JavaScript 中的相等性判断
The text was updated successfully, but these errors were encountered: