3 ms·
Objects in JavaScript use referential equality when checking for strict equality, which indexOf uses. In your code, 'elem' and '{foo:"bar"}' are different obje
by dsp1234 11y ago
Objects in JavaScript use referential equality when checking for strict equality, which indexOf uses. In your code, 'elem' and '{foo:"bar"}' are different objects which is why your code doesn't work as expected.
So
var x = {foo:"bar"}
var y = {foo:"bar"}
var z = x
x === y //false
x === z //true
This is specified in the ES3 document[0] at 11.9.6(13):
"13. Return true if x and y refer to the same object or if they refer to objects joined to each other (see
13.1.2). Otherwise, return false."
[0] - http://www.ecma-international.org/publications/files/ECMA-ST-ARCH/ECMA-262,%203rd%20edition,%20December%201999.pdf http://www.ecma-international.org/publications/files/ECMA-ST...