Object.is()
方法确定 2 个值是否为
相同值
.
Object.is(value1, value2);
value1
要比较的第 1 个值。
value2
要比较的第 2 个值。
布尔
指示 2 个自变量是否为相同值。
Object.is()
确定 2 个值是否为
相同值
. Two values are the same if one of the following holds:
undefined
null
true
or both
false
This is
not
the same as being equal according to the
==
operator. The
==
operator applies various coercions to both sides (if they are not the same Type) before testing for equality (resulting in such behavior as
"" == false
being
true
), but
Object.is
doesn't coerce either value.
This is also
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.NaN
as not equal to
NaN
.
if (!Object.is) {
Object.is = function(x, y) {
// SameValue algorithm
if (x === y) { // Steps 1-5, 7-10
// Steps 6.b-6.e: +0 != -0
return x !== 0 || 1 / x === 1 / y;
} else {
// Step 6.a: NaN == NaN
return x !== x && y !== y;
}
};
}
Object.is('foo', 'foo'); // true
Object.is(window, window); // true
Object.is('foo', 'bar'); // false
Object.is([], []); // false
var foo = { a: 1 };
var bar = { a: 1 };
Object.is(foo, foo); // true
Object.is(foo, bar); // false
Object.is(null, null); // true
// Special Cases
Object.is(0, -0); // false
Object.is(-0, -0); // true
Object.is(NaN, 0/0); // true
| 规范 |
|---|
|
ECMAScript (ECMA-262)
The definition of 'Object.is' in that specification. |
| Desktop | Mobile | Server | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
is
|
Chrome 30 | Edge 12 | Firefox 22 | IE No | Opera 17 | Safari 9 | WebView Android ≤37 | Chrome Android 30 | Firefox Android 22 | Opera Android 18 | Safari iOS 9 | Samsung Internet Android 2.0 | nodejs 0.10 |
完整支持
不支持
Object
Object.assign()
Object.create()
Object.defineProperties()
Object.defineProperty()
Object.entries()
Object.freeze()
Object.fromEntries()
Object.getOwnPropertyDescriptor()
Object.getOwnPropertyDescriptors()
Object.getOwnPropertyNames()
Object.getOwnPropertySymbols()
Object.getPrototypeOf()
Object.is()
Object.isExtensible()
Object.isFrozen()
Object.isSealed()
Object.keys()
Object.preventExtensions()
Object.prototype.__defineGetter__()
Object.prototype.__defineSetter__()
Object.prototype.__lookupGetter__()
Object.prototype.__lookupSetter__()
Object.prototype.hasOwnProperty()
Object.prototype.isPrototypeOf()
Object.prototype.propertyIsEnumerable()
Object.prototype.toLocaleString()
Object.prototype.toSource()
Object.prototype.toString()
Object.prototype.valueOf()
Object.seal()
Object.setPrototypeOf()
Object.values()
Function