The Object.is() method determines whether two values are the same value.
Return value: A Boolean indicating whether or not the two arguments are the same value.
我可以测试Object.is()
使用简单的类型,例如:
Object.is('hello', 'hello'); // true
Object.is(1, 1); // true
Object.is(null, null); // true
我还在 MDN 上看到我可以将它与“窗口”对象一起使用,如下所示:
Object.is(window, window); // true
当我尝试比较两个简单的“精确”值时,它似乎不起作用(或者可能我以错误的方式使用它)
拥有这 2 个简单的对象:
var o1 = {a: "a"};
var o2 = {a: "a"};
所有这些比较都返回 false!
Object.is(o1, o2); // false
Object.is(o1, {a: "a"}); // false
Object.is({a: "a"}, {a: "a"}); // false
您能帮忙解释一下为什么结果是错误的或者我如何使用 Object.is()
以错误的方式?
最佳答案
这是因为它们不是同一个引用。
注意:
var o1 = {a: "a"};
var o2 = o2;
Object.is(o1, o2);
结果为true
。
关于javascript - 为什么在比较 2 个 "exact"JavaScript 对象值时,Object.is() 返回 false?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42418009/