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.