This translation is incomplete. Please help translate this article from English.
This is a new technology, part of the ECMAScript 2015 (ES6) standard.
This technology's specification has been finalized, but check the compatibility table for usage and implementation status in various browsers.
El mètode Object.is() determina si dos valors tenen el mateix valor.
Sintaxi
Object.is(valor1, valor2);
Paràmetres
valor1- El primer valor a comparar.
valor2- El segon valor a comparar.
Valor de retorn
Un Boolean indica si els dos arguments tenen el mateix valor.
Descripció
Object.is() determina si dos valors tenen el mateix valor. Dos valors són iguals en cas que compleixi una de les condicions següents:
- ambdós són
undefined - ambdós són
null - ambdós són o bé
trueo béfalse - ambdós són cadenes amb la mateixa llargària i amb els mateixos caràcters
- ambdós són el mateix objecte
- ambdós són números i
Això no és el mateix que ser igualthe same as being equal d'acord amb l'operador ==. L'operador == 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.
Exemples
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 for non-ES6 browsers
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(x, y) {
// algoritme SameValue
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;
}
};
}
Especificacions
| Especificació | Estat | Comentaris |
|---|---|---|
| ECMAScript 2015 (6th Edition, ECMA-262) The definition of 'Object.is' in that specification. |
Standard | Definició inicial. |
Compatibilitat amb navegadors
| Característica | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
|---|---|---|---|---|---|
| Suport bàsic | 30 | 22 (22) | Not supported | (Yes) | Not supported |
| Característica | Android | Chrome per Android | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
|---|---|---|---|---|---|---|
| Suport bàsic | Not supported | ? | 22.0 (22) | Not supported | Not supported | Not supported |
Vegeu també
- Equality comparisons and sameness — a comparison of all three built-in sameness facilities