Note that there are some explanatory texts on larger screens.

plurals
  1. POHow does Javascript's indexOf() resolve references
    primarykey
    data
    text
    <p>I was confusing myself a little with a thought experiment and now I'm looking for some advice. Its about ECMAscript references and the <code>Array.prototype.indexOf()</code> method.</p> <p>Lets start easy:</p> <pre><code>var container = [ ]; // more code container.push( 5 ); container.push( 7 ); container.push( 10 ); </code></pre> <hr> <p>So now we pushed some "<em>primitive values</em>" 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</p> <pre><code>container.indexOf( 7 ); </code></pre> <p>will return <code>1</code> as expected. The big question I'm having is, if <code>.indexOf()</code> really compares the primitive value or if in reality a <code>Number()</code> object is created + stored and its <strong>reference</strong> is getting compared. This becomes a little more obvious if we re-write that like so:</p> <pre><code>var a = 5, b = 7, c = 10; var container = [ ]; container.push( a ); container.push( b ); container.push( c ); container.indexOf( b ); </code></pre> <hr> <p>Until this point, one could still easily argue that all <code>.indexOf()</code> needs to do is to compare values, but now lets look at something like this:</p> <pre><code>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 ); </code></pre> <p>Here, we filled that container array with <em>object-references</em> and still, <code>.indexOf()</code> works as expected</p> <pre><code>container.indexOf( b ) // === 1 </code></pre> <p>while a call like this</p> <pre><code>container.indexOf({ name: 'b', value: 10 }); </code></pre> <p>obviously returns <code>-1</code> since we are creating a new object and get a new reference. So here it must internally compare references with each other, right?</p> <p>Can some ECMAscript spec genius confirm that or even better link me some material about that ?</p> <p>A side question on this would be if there is any possibly way to access an internally stored object-reference within a <em>lexicalEnvironment</em> respectively <em>Activation Object</em>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload