Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The Mozilla implementations just try to emulate exactly the steps that are described in the specification, <code>Object(this);</code> emulates the <em>first step</em>, calling the <a href="http://es5.github.com/#x9.9" rel="noreferrer"><code>ToObject</code></a> internal method:</p> <p>From <code>Array.prototype.forEach</code> <a href="http://es5.github.com/#x15.4.4.18" rel="noreferrer">15.4.4.18</a>:</p> <blockquote> <p>....</p> <p>When the forEach method is called with one or two arguments, the following steps are taken:</p> <ol> <li><p>Let O be the result of calling ToObject passing the this value as the argument.</p></li> <li><p>Let lenValue be the result of calling the [[Get]] internal method of O with the argument "length".</p></li> <li><p>Let len be ToUint32(lenValue).</p></li> </ol> <p>....</p> </blockquote> <p>Calling <a href="http://es5.github.com/#x15.2.1" rel="noreferrer">the Object constructor as a function</a> behind the scenes it performs type conversion, internally as described in <a href="http://es5.github.com/#x15.2.1.1" rel="noreferrer">15.2.1.1</a> the <code>ToObject</code> method is called.</p> <p>There are more things like this if you look carefully, for example, the line:</p> <pre><code>var len = t.length &gt;&gt;&gt; 0; </code></pre> <p>They are emulating a call to the <a href="http://es5.github.com/#x9.6" rel="noreferrer">ToUint32</a> internal method, as described in the step 3, using the unsigned right shift operator (<a href="http://es5.github.com/#x11.7.3" rel="noreferrer"><code>&gt;&gt;&gt;</code></a>). </p> <p><strong>Edit:</strong> The previous lines answer <em>why</em> the Mozilla implementation does it in this way.</p> <p>You might wonder why the ECMAScript spec. needs to call <code>ToObject</code>, check back the <em>Step 2</em>, and it will start to seem obvious:</p> <blockquote> <ol start="2"> <li>Let lenValue be the result of calling the [[Get]] internal method of O with the argument "length".</li> </ol> </blockquote> <p>The spec. needs to ensure that the <code>this</code> value used when the function is called is <em>an object</em>, because primitive values don't have any internal methods, an as you can see on the step 2, the <code>[[Get]](P)</code> internal method is needed, to get the value of the <code>length</code> property.</p> <p>This is done because for <em>strict functions</em> (and also for built-in functions), you can set <em>primitive values</em> as the function's <code>this</code> value, e.g.:</p> <pre><code>(function () {"use strict"; return typeof this; }).call(5); // "number" </code></pre> <p>While for non-strict functions, the <code>this</code> value is always converted to Object:</p> <pre><code>(function () { return typeof this; }).call(5); // "object" </code></pre>
    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.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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