Diese Übersetzung ist unvollständig. Bitte helfen Sie, diesen Artikel aus dem Englischen zu übersetzen.
This is an experimental technology, part of the ECMAScript 6 (Harmony) proposal.
Because this technology's specification has not stabilized, check the compatibility table for usage in various browsers. Also note that the syntax and behavior of an experimental technology is subject to change in future version of browsers as the spec changes.
Summary
The Object.is() method determines whether two values are the same value.
Syntax
var isSame = Object.is(value1, value2);
Parameters
-
value1 - The first value to compare.
-
value2 - The second value to compare.
Beschreibung
Object.is() bestimmt, ob zwei Werte gleich sind. Dabei gelten zwei Werte genau dann als gleich, wenn eine der folgenden Bedingungen zutrifft:
- beide sind
undefined - beide sind
null - beide sind
trueoder beide sindfalse - beide sind Strings mit selber Länge und den selben Zeichen
- beide sind das selbe Objekt
- beide sind Zahlen und eine der folgenden Bedingungen trifft zu:
- beide sind
+0 - beide sind
-0 - beide sind
NaN - beide sind nicht Null, beide sind nicht
NaNund beide haben den selben Zahlenwert
- beide sind
Dies ist nicht das selbe wie der ==-Operator. Dieser verwendet verschiedene, situationsabhängige Typ-Umwandlungen auf beiden Seiten bevor auf Gleichheit getestet wird (was z.B. dazu führt, dass der Ausdruck "" == false zu true ausgewertet wird). Object.is hingegen wandelt keinen der beiden Werte um.
Es ist ebenfalls nicht das selbe wie der strikte ===-Operator. Dieser – ebenso wie der ==-Operator – behandelt zum Beispiel +0 und -0 als identisch während NaN und NaN als nicht identisch behandelt werden.
Examples
Object.is('foo', 'foo'); // true
Object.is(window, window); // true
Object.is('foo', 'bar'); // false
Object.is([], []); // false
var test = { a: 1 };
Object.is(test, test); // true
Object.is(null, null); // true
// Special Cases
Object.is(0, -0); // false
Object.is(-0, -0); // true
Object.is(NaN, 0/0); // true
Polyfill
Object.is is a proposed addition to the ECMA-262 standard; as such it may not be present in all browsers. You can work around this by using the following code at the beginning of your scripts. This will allow you to use Object.is when there is no built–in support.
if (!Object.is) {
Object.is = function(v1, v2) {
if (v1 === 0 && v2 === 0) {
return 1 / v1 === 1 / v2;
}
if (v1 !== v1) {
return v2 !== v2;
}
return v1 === v2;
};
}
Specifications
| Specification | Status | Comment |
|---|---|---|
| ECMAScript 6 (ECMA-262) Die Definition von 'Object.is' in dieser Spezifikation. |
Anwärter Empfehlung | Initial definition. |
Browser compatibility
| Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
|---|---|---|---|---|---|
| Basic support | 30.0.1599.114 | 22 (22) | Nicht unterstützt | (Ja) | Nicht unterstützt |
| Feature | Android | Chrome for Android | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
|---|---|---|---|---|---|---|
| Basic support | Nicht unterstützt | ? | 22.0 (22) | Nicht unterstützt | Nicht unterstützt | Nicht unterstützt |
See also
- JavaScript Guide: Sameness — a comparison of all three built-in sameness facilities