Il metodo Object.is()
determina se i due parametri di input hanno lo stesso valore.
Sintassi
Object.is(value1, value2);
Parametri
value1
- Il primo valore da comparare.
value2
- Il secondo valore da comparare.
Return value
A Boolean
indicating whether or not the two arguments are the same value.
Descrizione
Object.is()
determina se due valori sono uguali. Due valori sono uguali se sono :
- entrambi
undefined
- entrambi
null
- entrambi
true
o entrambifalse
- entrambi stringhe della stessa lunghezza con gli stessi caratteri
- entrambi lo stesso oggetto
- entrambi numeri ed
Questo non è la stessa uguaglianza dell'operatore ==
. L'operatore ==
applica varie conversioni ad entrambi (se non sono dello stesso tipo) prima di testare l'uguaglianza (ad esempio, "" == false
risultando true
), ma Object.is
non converte i loro valori.
Inoltre questo non è la stessa uguaglianza dell'operatore ===
. L'operatore ===
(ed anche l'operatore ==
) trattano i numeri -0
e +0
come uguali e trattano Number.NaN
differentemente da NaN
.
Esempi
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
// Casi speciali
Object.is(0, -0); // false
Object.is(-0, -0); // true
Object.is(NaN, 0/0); // true
Polyfill
if (!Object.is) {
Object.is = function(x, y) {
// Algoritmo 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;
}
};
}
Specifiche
Specification | Status | Comment |
---|---|---|
ECMAScript 2015 (6th Edition, ECMA-262) The definition of 'Object.is' in that specification. |
Standard | Definizione iniziale. |
ECMAScript (ECMA-262) The definition of 'Object.is' in that specification. |
Living Standard |
Compatibilità coi browser
BCD tables only load in the browser
La compatibility table su questa pagina è generata da dati strutturali. Se vuoi contribuire per i dati, puoi visitare https://github.com/mdn/browser-compat-data ed inviarci una pull request.
Vedi anche
- Equality comparisons and sameness (in inglese) — un paragone di tutte e tre le facilitazioni per comparare uguaglianze