Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><code>var t = Object(this);</code></p> <p>Is based on the ES5 specification. It specifically state that <code>this</code> should be passed into the <code>Object</code> constructor.</p> <p>If you look closely at the exact algorithm specified in the ES5 spec then the method provided by Mozilla mirrors it almost exactly (it's limited by ES3 features). This is the reason this code seems to have a few quirks.</p> <p>Here is the ES5 specification:</p> <blockquote> <p>When the map method is called with one or two arguments, the following steps are taken:</p> </blockquote> <ol> <li>Let O be the result of calling ToObject passing the this value as the argument. </li> <li>Let lenValue be the result of calling the [[Get]] internal method of O with the argument "length". </li> <li>Let len be ToUint32(lenValue). </li> <li>If IsCallable(callbackfn) is false, throw a TypeError exception. </li> <li>If thisArg was supplied, let T be thisArg; else let T be undefined. </li> <li>Let A be a new array created as if by the expression new Array(len) where Array is the standard builtin constructor with that name and len is the value of len. </li> <li>Let k be 0. </li> <li><p>Repeat, while k &lt; len</p> <ol> <li>Let Pk be ToString(k). </li> <li>Let kPresent be the result of calling the [[HasProperty]] internal method of O with argument Pk. </li> <li>If kPresent is true, then <ul> <li>Let kValue be the result of calling the [[Get]] internal method of O with argument Pk. </li> <li>Let mappedValue be the result of calling the [[Call]] internal method of callbackfn with T as the this value and argument list containing kValue, k, and O. </li> <li>Call the [[DefineOwnProperty]] internal method of A with arguments Pk, Property Descriptor {[[Value]]: mappedValue, [[Writable]]: true, [[Enumerable]]: true, [[Configurable]]: true}, and false. </li> <li>Increase k by 1. </li> </ul></li> </ol></li> <li>Return A.</li> </ol> <blockquote> <p>Let O be the result of calling ToObject passing the this value as the argument.</p> </blockquote> <p>Notice that step one specifically says you should call <code>Object(this)</code></p> <blockquote> <p>Let lenValue be the result of calling the [[Get]] internal method of O with the argument "length". </p> <p>Let len be ToUint32(lenValue).</p> </blockquote> <p>Step 2 and 3 specifically say get <code>t.length</code> then call <code>ToUint32</code> which is implemented as <code>&gt;&gt;&gt; 0</code> here.</p> <p>The actual signature mentioned in the specification is</p> <blockquote> <p>Array.prototype.map ( callbackfn [ , thisArg ] ) </p> </blockquote> <p>In the above signature <code>callbackfn</code> is a required argument and <code>[ ]</code> is an array of optional arguments which only contains one <code>thisArg</code>.</p> <p>Mozilla have mirrored this in their definition of <code>function(fun /*, thisp */) {</code> to specify that <code>thisp</code> is an optional argument and it's clear this is the case from the function signature rather then from looking at the code.</p>
 

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