Can somebody explain this Javascript method?

Original source:

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

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

-------------Problems Reply------------

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


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


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

x= [].reverse;

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.


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:

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.

Category:javascript Views:0 Time:2010-02-14

Related post

  • Is the 'on' method in this node.js code a JavaScript method or a node method? 2011-11-18

    I couldn't find this answer on Google because 'on' is such a common word. In this node.js example: conn.on('close', function() { var pos = connections.indexOf(conn); if (pos >= 0) { connections.splice(pos, 1); } }); There is a .on method(?). What

  • how to combine similar javascript methods to one 2009-04-30

    I have an codebehind page linking several checkboxes to javascript methods. I want to make only one javascript method to handle them all since they are the same logic, how would i do this? code behind page load: checkBoxShowPrices.Attributes.

  • How can I call javascript method from my aspx.cs file 2009-05-02

    I have a javascript method which I have to call from the aspx page at the time of page load. --------------Solutions------------- Javascript methods are client-side methods, so you can not call them in your server side code. But if you're looking for

  • Call Page Javascript Methods from WatiN? 2009-05-28

    I am just experimenting WatiN unit testing tool for ASP.NET application.. And my pages are designed with huge & complex list of own custom controls .. And its very difficult for us to identify the elements within these control (for ex: to select

  • Can someone please explain this JavaScript regular expression for me? 2009-06-24

    Can someone please explain this JavaScript regular expression for me? new RegExp("(^|\\s+)" + className + "(\\s+|$)"), ' ') --------------Solutions------------- ( Either ^ the start of the string | or \\s+ one or more whitespace characters ) followed

  • How to recognize the JavaScript method(function) 2009-07-14

    I am writing a Plug-In for VS2008 and I would like to recognize the JavaScript method(function). I have a file - sample.js:` function test0() { var i = 0; { var j = 0; } var array = { 1: 2, dd: 10, aaa: 3 }; return array; } function test1() { var ii

  • Applet not seeing javascript methods every time 2009-08-04

    I have an applet that does extensive communication with javascript and the browser. Now, I've stuck a JSObject call in the init method of the applet to notify the browser that the applet is there and good to go, but I've noticed something strange. Oc

  • How do I make a textbox and button so it will fire a javascript method and not post? 2009-10-20

    How do I create a textbox and button in HTML so that when I click it, it will fire a javascript method with the value and output the result below the textbox/button? And if you click again, it will continue to add new lines of output below the textbo

  • Call JavaScript method on dynamic form element 2009-12-14

    I am creating new textbox using JavaScript. I want to call a JavaScript method onkeyup event of this newly created textbox. How will I do this? Below is my code: var element2 = document.createElement("input"); element2.type = "text";'com

  • how to call a javascript method after action or from a view? 2010-02-12

    i just want to be able to call a javascritp subroutine on the client, after the server has done its thing (when an action completes or control goes to the view, i'd dont mind calling the js from the view either). for some reason, even vs2010 doesn't

  • Execute an method from JavaScript method 2010-03-04

    I have a javascript method looks like this JSMethod(JS_para_1,JS_para_2) { ...... ,,,,, } and I have an ASP.NET method like this ASP_Net_Method(ASP_Para_1,ASP_Para_2) { .... ,,, } Now I want to call this ASP_Net_Method from my JSMethod by passing som

  • execute javascript method after method excution complete? 2010-05-13

    I want execute below callback()method after completion of process document.getElementById('btnDownload').click(); method. click() is the code behind method. That means, I mean after complete process of click() then excute callback() method. Because m

  • JSF, Richfaces: How to run Javascript method each time rich:datatable (re)renders? 2010-05-21

    I have a rich:datatable which may be reRendered my multiple components in my page. I want to run a Javascript method each time it is rendered, including the first time the page loads. How can I do this? --------------Solutions------------- You can ca

  • Get list of Javascript methods from C# WebBrowser or WatiN 2010-06-10

    After a page loads, I'm trying to get a list of all JavaScript methods that exist on that page. I know how to invoke Javascript from C# and C# from Javascript, the problem is that I can't seem to find a way to get the original list of Javascript meth

  • Error when calling javascript method from applet 2010-06-17

    I am trying to call javascript method from an Applet using in the applet: JSObject window = JSObject.getWindow(this.Class); Object[] args = .... //arguments"javascriptMethodName", args); But I get the exception a

  • How to validate many forms in html using single javascript method? 2010-06-24

    I am having two forms for example login and register. In login page i am having two fields namely username and password. In register page i am having fields namely firstname, lastname, username, dob, and password. Here we can able to validate the abo

  • How to know where javascript method is defined and which method is called using firebug 2010-07-19

    How can we know which javascript method is called and where it is defined? (When methods are attached dynamically) Let us consider situation where JQuery Bind method is used to bind an event. If I see control in FireBug with FireQuery, I can see even

  • When fine-tuning performance, what is the best way to call JavaScript methods multiple times? 2010-08-28

    I have been researching JavaScript performance. I've learned that when accessing more than once, it is usually best to copy closure variables and class members into local scope to speed things up. For example: var i = 100; var doSomething = function

  • javascript method post - how to call mvc action onClick? 2010-09-08

    i have an image: <img onclick="Search()" alt="searchPage" style="vertical-align: middle;" height="17px" src="../../Stylesheets/search.PNG" title="search" /> and javascript methode: <script language="javascript" type="text/javascript"> fun

Copyright (C), All Rights Reserved.

processed in 0.095 (s). 11 q(s)