一个变量与自身比较 [英] Comparing a variable with itself

查看:125
本文介绍了一个变量与自身比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我绊了这种填充工具Array.prototype.includes的。
<一href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/includes\">https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/includes.
是否有变量与自己对线21,22比较的原因是什么?

 如果(searchElement === currentElement ||
         (searchElement == searchElement和放大器;!&安培;!currentElement == currentElement)){
  返回true;
}


解决方案

是,这第二个操作数的 || 并检查是否既 searchElement currentElement 是的 NaN的 - 的的仅在JavaScript值不是 === 本身。 包括应该使用的 SameValueZero 的等价算法,这是从的的 全等比较算法(使用<一个href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Comparison_Operators#Identity\"相对=nofollow> === )或的 EM> SameValue 的算法(中的 Object.is )。

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;
}

解决方案

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆