将变量与自身进行比较 [英] Comparing a variable with itself
问题描述
我偶然发现了这个 Array.prototype.includes 的 polyfill.https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/includes.21,22行的变量和自己比较有没有道理?
I stumbled over this polyfill of Array.prototype.includes. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/includes. Is there a reason for the comparison of the variables with themselves on line 21,22?
if (searchElement === currentElement ||
(searchElement !== searchElement && currentElement !== currentElement)) {
return true;
}
推荐答案
是的,||
的第二个操作数确实检查了 searchElement
和 currentElement
是 NaN
- JavaScript 中的 唯一值 不是 ===
给自己.includes
应该使用 SameValueZero
等价算法,区别于严格相等比较算法(由 ===
) 或 SameValue
算法(用于Object.is
).
Yes, this second operand of the ||
does check whether both searchElement
and currentElement
are NaN
- the only value in JavaScript that is not ===
to itself. includes
is supposed to use the SameValueZero
equivalence algorithm, which is different from the the Strict Equality Comparison Algorithm (used by ===
) or the SameValue
algorithm (used in Object.is
).
这篇关于将变量与自身进行比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!