javascript Object.is vs ===

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30543190/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 12:20:59  来源:igfitidea点击:

Object.is vs ===

javascriptecmascript-6

提问by JS-JMS-WEB

I stumbled upon a code example which was using this comparison:

我偶然发现了一个使用这种比较的代码示例:

var someVar = 0;
Object.is(false, someVar); //Returns false 

I know false == 0will be truethat's why we have ===.

我知道false == 0会是true这就是为什么我们有===

How is Object.isdifferent from ===?

如何Object.is不同===

回答by Gurpreet Singh

===is called strict comparison operator in JavaScript. Object.isand strict comparison operator behave exactly the same except for NaNand +0/-0.

===在 JavaScript 中称为严格比较运算符。Object.is除了NaNand之外,和严格比较运算符的行为完全相同+0/-0

From MDN:

来自 MDN:

Object.is()method is not the same as being equal according to the ===operator. The ===operator (and the ==operator as well) treats the number values -0 and +0 as equal and treats Number.NaNas not equal to NaN.

Object.is()方法与根据===运算符相等不同。的===操作者(和==操作员以及)对待数值-0和0为相等和治疗Number.NaN为不等于NaN

Code below highlights the difference between ===and Object.is().

下面的代码突出了===和之间的区别Object.is()

console.log(+0 === -0); //true
console.log(Object.is(+0, -0)); //false

console.log(NaN === NaN); // false
console.log(Object.is(NaN, NaN)); //true

console.log(Number.NaN === Number.NaN); // false
console.log(Object.is(Number.NaN, Number.NaN)); // true

console.log(NaN === Number.NaN); // false
console.log(Object.is(NaN, Number.NaN)); // true

enter image description here

在此处输入图片说明

You can find more examples here.

您可以在此处找到更多示例。

Note: Object.isis part of the ECMAScript 6 proposal and is not widely supported yet (e.g. it's not supported by any version of Internet Explorer or many older versions of other browsers). However you can use a polyfill for non-ES6 browsers which can be found in link given above.

注意Object.is是 ECMAScript 6 提案的一部分,尚未得到广泛支持(例如,任何版本的 Internet Explorer 或许多旧版本的其他浏览器都不支持)。但是,您可以为非 ES6 浏览器使用 polyfill,可以在上面给出的链接中找到。

回答by T.J. Crowder

Object.isuses the specification's SameValue algorithm, whereas ===uses the Strict Equality Algorithm. A note on the Strict Equality Algorithm calls out the difference:

Object.is使用规范的SameValue algorithm,而===使用Strict Equality Algorithm。关于严格相等算法的注释指出了不同之处:

This algorithm differs from the SameValue Algorithm...in its treatment of signed zeroes and NaNs.

该算法与 SameValue 算法的不同之处在于它对有符号零和 NaN 的处理。

Note that:

注意:

  • NaN === NaNis false, but Object.is(NaN, NaN)is true
  • +0 === -0is true, but Object.is(+0, -0)is false
  • -0 === +0is true, but Object.is(-0, +0)is false
  • NaN === NaN是假的,但是Object.is(NaN, NaN)是真的
  • +0 === -0是真的,但是Object.is(+0, -0)是假的
  • -0 === +0是真的,但是Object.is(-0, +0)是假的

JavaScript has at leastfour kinds of "equality":

JavaScript至少有四种“相等”:

  • "Loose" (==), where the operands will be coerced to try to make them match. The rules are clearly specified, but non-obvious. ("" == 0is true; "true" == trueis false, ...).
  • "Strict" (===), where operands of differing types will not be coerced (and will not be equal), but see note above about NaNand positive and negative zero.
  • SameValue - as listed above (used by Object.is).
  • SameValueZero - like SameValueexcept +0and -0are the same instead of different (used by Mapfor keys, and by Array.prototype.includes).
  • “松散” ( ==),其中操作数将被强制尝试使它们匹配。规则明确规定,但不明显。("" == 0true"true" == truefalse,...)。
  • “严格” ( ===),其中不同类型的操作数不会被强制(并且不会相等),但请参阅上面关于NaN正零和负零的注释。
  • SameValue - 如上所列(由 使用Object.is)。
  • SameValueZero - 就像SameValueexcept+0-0是相同的而不是不同的(用于Map键和 by Array.prototype.includes)。

There's also object equivalence, which isn't provided by the language or runtime itself, but is usually expressed as: The objects have the same prototype, same properties, and their property values are the same (by some reasonable definition of "the same").

还有object equivalence,它不是由语言或运行时本身提供的,但通常表示为:对象具有相同的原型,相同的属性,并且它们的属性值相同(通过“相同”的一些合理定义)。



SameValue algorithm:

SameValue 算法

  • If Type(x) is different from Type(y), return false.
  • If Type(x) is Number, then
    • If x is NaN and y is NaN, return true.
    • If x is +0 and y is -0, return false.
    • If x is -0 and y is +0, return false.
    • If x is the same Number value as y, return true.
    • Return false.
  • Return SameValueNonNumber(x, y).
  • 如果 Type(x) 与 Type(y) 不同,则返回 false。
  • 如果 Type(x) 是 Number,则
    • 如果 x 是 NaN 并且 y 是 NaN,则返回 true。
    • 如果 x 为 +0 且 y 为 -0,则返回 false。
    • 如果 x 为 -0 且 y 为 +0,则返回 false。
    • 如果 x 与 y 的 Number 值相同,则返回 true。
    • 返回假。
  • 返回 SameValueNonNumber(x, y)。

...where SameValueNonNumberis:

...其中SameValueNonNumber是:

  • Assert: Type(x) is not Number.
  • Assert: Type(x) is the same as Type(y).
  • If Type(x) is Undefined, return true.
  • If Type(x) is Null, return true.
  • If Type(x) is String, then
    • If x and y are exactly the same sequence of code units (same length and same code units at corresponding indices), return true; otherwise, return false.
  • If Type(x) is Boolean, then
    • If x and y are both true or both false, return true; otherwise, return false.
  • If Type(x) is Symbol, then
    • If x and y are both the same Symbol value, return true; otherwise, return false.
  • Return true if x and y are the same Object value. Otherwise, return false.
  • 断言:Type(x) 不是数字。
  • 断言:Type(x) 与 Type(y) 相同。
  • 如果 Type(x) 未定义,则返回 true。
  • 如果 Type(x) 为 Null,则返回 true。
  • 如果 Type(x) 是 String,则
    • 如果 x 和 y 是完全相同的代码单元序列(相同的长度和相应索引处的相同代码单元),则返回 true;否则,返回false。
  • 如果 Type(x) 是布尔值,则
    • 如果 x 和 y 都为真或都为假,则返回真;否则,返回false。
  • 如果 Type(x) 是 Symbol,则
    • 如果 x 和 y 都是相同的 Symbol 值,则返回 true;否则,返回false。
  • 如果 x 和 y 是相同的 Object 值,则返回 true。否则,返回false。


Strict Equality Algorithm:

严格相等算法

  1. If Type(x) is different from Type(y), return false.
  2. If Type(x) is Number, then
    • If x is NaN, return false.
    • If y is NaN, return false.
    • If x is the same Number value as y, return true.
    • If x is +0 and y is -0, return true.
    • If x is -0 and y is +0, return true.
    • Return false.
  3. Return SameValueNonNumber(x, y).
  1. 如果 Type(x) 与 Type(y) 不同,则返回 false。
  2. 如果 Type(x) 是 Number,则
    • 如果 x 是 NaN,则返回 false。
    • 如果 y 是 NaN,则返回 false。
    • 如果 x 与 y 的 Number 值相同,则返回 true。
    • 如果 x 为 +0 且 y 为 -0,则返回 true。
    • 如果 x 为 -0 且 y 为 +0,则返回 true。
    • 返回假。
  3. 返回 SameValueNonNumber(x, y)。

回答by Isaac

Object.is = function(v1, v2){
  //test for `-0`
  if(v1 === 0 && v2 === 0) {
    return 1 / v1 === 1 / v2;
  }
  
  //test for `NaN`
  if(v1 !== v1) {
    return v2 !== v2;
  }
  
  //everything else
  return v1 === v2;
}

The above is the polyfill function to show how Object.isworks, for anyone who are interested to know. A reference to You-Don't-Know-JS

以上是 polyfill 函数,展示了它是如何Object.is工作的,有兴趣的可以了解一下。对You-Don't-Know-JS 的引用

回答by Willem van der Veen

Summary:

概括:

The Object.is()function takes 2 values as arguments and returns true if the 2 given values are exact the same, otherwise it will return false.

Object.is()函数将 2 个值作为参数,如果 2 个给定值完全相同,则返回 true,否则返回 false。

Why do we need this?

我们为什么需要这个?

You might think, we already have strict equality (checks type + value) checking in javascript with the ===operator, why do we need this function? Well strict equality isn't sufficient in some cases and they are the following:

您可能会想,我们已经在 javascript 中使用===运算符检查了严格相等(检查类型 + 值),为什么我们需要这个函数?在某些情况下,严格平等是不够的,它们如下:

console.log(NaN === NaN);   // false
console.log(-0 === +0);     // true

Object.is()helps us by being able to compare these values to see if they are similar, something the strict equality operator cannot do.

Object.is()通过比较这些值以查看它们是否相似来帮助我们,这是严格相等运算符无法做到的。

console.log(Object.is(NaN, NaN));  // true
console.log(Object.is(-0, 0));     // false
console.log(Object.is(+0, +0));    // true
console.log(Object.is(+0, -0));    // false

回答by Alireza

In a nutshell, they are similar, but Object.isis smarter and more accurate...

简而言之,它们很相似,但Object.is更智能、更准确……

Let's look at this...

让我们看看这个...

+0 === -0 //true

But this is not fully right as it ignores -and +before...

但是,这并不完全正确,因为它忽略了-+之前...

Now we use:

现在我们使用:

Object.is(+0, -0) //false

As you see, this is more accurate to compare.

如您所见,这样比较更准确。

Also in case of NaNthat works more like correct, as consider any NaNthe same.

此外,在这种情况下NaN,工作更像是正确的,因为考虑任何NaN相同。