简体   繁体   中英

How does Javascript's indexOf() resolve references

I was confusing myself a little with a thought experiment and now I'm looking for some advice. Its about ECMAscript references and the Array.prototype.indexOf() method.

Lets start easy:

var container = [ ];
// more code
container.push( 5 );
container.push( 7 );
container.push( 10 );

So now we pushed some " primitive values " into our ECMAscript array (whether or not that statement is true I'll come back for), at least I imagined it like this so far. A call to

container.indexOf( 7 );

will return 1 as expected. The big question I'm having is, if .indexOf() really compares the primitive value or if in reality a Number() object is created + stored and its reference is getting compared. This becomes a little more obvious if we re-write that like so:

var a = 5,
    b = 7,
    c = 10;

var container = [ ];

container.push( a );
container.push( b );
container.push( c );

container.indexOf( b );

Until this point, one could still easily argue that all .indexOf() needs to do is to compare values, but now lets look at something like this:

var a = { name: 'a', value: 5 },
    b = { name: 'b', value: 10 },
    c = { name: 'c', value: 15 };

var container = [ ];
// more code
container.push( a );
container.push( b );
container.push( c );

Here, we filled that container array with object-references and still, .indexOf() works as expected

container.indexOf( b ) // === 1

while a call like this

container.indexOf({ name: 'b', value: 10 });

obviously returns -1 since we are creating a new object and get a new reference. So here it must internally compare references with each other, right?

Can some ECMAscript spec genius confirm that or even better link me some material about that ?

A side question on this would be if there is any possibly way to access an internally stored object-reference within a lexicalEnvironment respectively Activation Object .

It boils down to indexOf() comparing against each array property in turn using the same algorithm as the === operator.

The relevant section of the ECMAScript 5 spec is section 15.4.4.14, step 9, section b (highlighting mine):

If kPresent is true, then

i. Let elementK be the result of calling the [[Get]] internal method of O with the argument ToString(k).

ii. Let same be the result of applying the Strict Equality Comparison Algorithm to searchElement and elementK.

iii. If same is true, return k.

References:

I'm not sure if this is guaranteed across all ECMAScript implementations or not, but the Mozilla documentation states that it uses strict equality to make the comparison (===). As such this would exhibit the behaviour you describe, comparing by values on primitives, but by reference on objects (see strict equality ).

@Tim Down is right. indexOf does strict comparison. I am giving a demonstration of this by overriding valueOf function

var MyObject = function(n, v){
   this.name = n;
   this.value = v;
}

MyObject.prototype.valueOf = function(){
    return this.value;
}

var a = new MyObject("a", 5);
var b = new MyObject("b", 10);
var c = new MyObject("c", 15);

var container = [ ];

container.push( a );
container.push( b );
container.push( c );

console.log(b == 10); // true
console.log(container[1] == 10); // true

console.log(b === 10); // false
container.indexOf(10); // -1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM