Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This is to do with the weird way <code>this</code> binding works in JavaScript.</p> <pre><code>[].reverse </code></pre> <p>is the method <code>reverse</code> on an empty list. If you call it, through one of:</p> <pre><code>[].reverse(); []['reverse'](); ([].reverse)(); </code></pre> <p>then it executes with <code>this</code> bound to the list instance <code>[]</code>. But if you detach it:</p> <pre><code>x= [].reverse; x(); </code></pre> <p>it executes with no <code>this</code>-binding, so <code>this</code> in the function points to the global (<code>window</code>) object, in one of JavaScript's worst, most misleading design mistakes.</p> <pre><code>(x=[].reverse)() </code></pre> <p>Is also doing the detach. The assignment operator returns the same function object it was passed so it looks like it's doing nothing, but it has the side-effect of breaking the limited special case that causes JavaScript to bind <code>this</code>.</p> <p>So you are saying:</p> <pre><code>Array.prototype.reverse.call(window) </code></pre> <p><code>reverse</code>, like many other <code>Array.prototype</code> methods, is defined by ECMAScript to work on any native sequence-like object. It reverses the items with number-string keys (up to <code>object.length</code>) and returns the object. So it'll return the object that was passed in for any type that has a <code>length</code> property.</p> <p><code>window</code> has a length property, which corresponds to <code>window.frames.length</code>, so calling this method with <code>this</code> pointing at <code>window</code> will work and return the <code>window</code>. In theory it may still fail, because:</p> <ol> <li><code>window</code> is allowed to be a “host object” rather than a “native object”; in this case the guarantees about what you can pass to other prototypes' methods don't necessarily apply; and</li> <li>if the window actually has frames/iframes, it would try to reverse their order, which wouldn't work because the frame collection is read-only.</li> </ol> <p>However, in current browsers the former case does work and the latter fails silently without an error, so you still get the <code>===window</code> behaviour and not an Exception.</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.
    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