23

Original source: http://twitter.com/tobeytailor/status/8998006366

(x=[].reverse)() === window // true

I've noticed that this behavior affects all the native types. What exactly is happening here?

3
  • 1
    [].reverse.call(y) === y for all scalars y.
    – kennytm
    Feb 14, 2010 at 14:19
  • 4
    I've noticed that this behavior... What behavior?
    – Cheeso
    Feb 14, 2010 at 14:25
  • This now throws an error in more modern engines since Array.prototype.reverse is set in strict mode, where an undefined this value is actually undefined and no longer globalThis (i.e. window in browsers, global in Node.js). Jul 11, 2021 at 17:33

1 Answer 1

41

This is to do with the weird way this binding works in JavaScript.

[].reverse

is the method reverse on an empty list. If you call it, through one of:

[].reverse();
[]['reverse']();
([].reverse)();

then it executes with this bound to the list instance []. But if you detach it:

x= [].reverse;
x();

it executes with no this-binding, so this in the function points to the global (window) object, in one of JavaScript's worst, most misleading design mistakes.

(x=[].reverse)()

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 this.

So you are saying:

Array.prototype.reverse.call(window)

reverse, like many other Array.prototype methods, is defined by ECMAScript to work on any native sequence-like object. It reverses the items with number-string keys (up to object.length) and returns the object. So it'll return the object that was passed in for any type that has a length property.

window has a length property, which corresponds to window.frames.length, so calling this method with this pointing at window will work and return the window. In theory it may still fail, because:

  1. window 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
  2. 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.

However, in current browsers the former case does work and the latter fails silently without an error, so you still get the ===window behaviour and not an Exception.

3
  • 1
    Do you happen to know why it is that somebody would code that up? I mean, I understand why the effect happens of course, but why would I put that in my code? What does it achieve?
    – Pointy
    Feb 14, 2010 at 15:23
  • 4
    Absolutely nothing. It's just showing off one of JavaScript's many unexpected behaviours. If I found this in real code I'd worry!
    – bobince
    Feb 14, 2010 at 16:15
  • 2
    A possible reason to do this might be to check if some other script messed with the window object (window = open('http://google.com/') is enough). var window = (x=[].reverse)(); could restore it locally.
    – silviot
    Jan 2, 2011 at 10:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.