--- title: Object.is() slug: Web/JavaScript/Reference/Global_Objects/Object/is tags: - Comparazione - Condizionale - Condizione - ECMAScript 2015 - Equalità - Italiano - JavaScript - Oggetto - Uguaglianza - metodo translation_of: Web/JavaScript/Reference/Global_Objects/Object/is ---
Il metodo Object.is()
determina se i due parametri di input hanno lo stesso valore.
Object.is(value1, value2);
value1
value2
A {{jsxref("Boolean")}} indicating whether or not the two arguments are the same value.
Object.is()
determina se due valori sono uguali. Due valori sono uguali se sono :
true
o entrambi false
+0
-0
Questo non è la stessa uguaglianza dell'operatore {{jsxref("Operators/Comparison_Operators", "==", "#Equality")}}. L'operatore {{jsxref("Operators/Comparison_Operators", "==", "#Equality")}} 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 {{jsxref("Operators/Comparison_Operators", "===", "#Identity")}}. L'operatore {{jsxref("Operators/Comparison_Operators", "===", "#Identity")}} (ed anche l'operatore {{jsxref("Operators/Comparison_Operators", "==", "#Equality")}}) trattano i numeri -0
e +0
come uguali e trattano {{jsxref("Number.NaN")}} differentemente da {{jsxref("NaN")}}.
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
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; } }; }
Specification | Status | Comment |
---|---|---|
{{SpecName('ES2015', '#sec-object.is', 'Object.is')}} | {{Spec2('ES2015')}} | Definizione iniziale. |
{{SpecName('ESDraft', '#sec-object.is', 'Object.is')}} | {{Spec2('ESDraft')}} |
{{Compat("javascript.builtins.Object.is")}}