Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7723483
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T04:27:17+00:00 2026-06-01T04:27:17+00:00

Consider the code: window.a = function(x){ var r = x*2; window.a =alert; // redefines

  • 0

Consider the code:

    window.a = function(x){ 
        var r = x*2; 
        window.a =alert; // redefines itself after first call
        return r;
    }; 
    a('2 * 2 = '+a(2)); // doesn't work. it should've alerted "2 * 2 = 4"

This doesn’t work either:

    window.a = function(x){ 
        alert(x); 
        window.a = function(x){ // redefines itself after first call
            var r = x*2; 
            return r;   
        }
    }; 
    a('2 * 2 = '+a(2)); // doesn't work. it should've alerted "2 * 2 = 4"

As neither does this:

    window.a = function(x){ alert(x); window.c = window.a; window.a = window.b; window.b = window.c; };
    window.b = function(x){ var r = x*2; window.c = window.b; window.b = window.a; window.a = window.c; return r; };
    a('2 * 2 = '+a(2)); // doesn't work. 

And basically I’ve tried all possible ways and neither seem to do the job. Can someone please explain why?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T04:27:19+00:00Added an answer on June 1, 2026 at 4:27 am

    You are successfully redefining the function, it’s just that the expression calling it has already grabbed the old function reference: The first thing that happens in a call expression is that the thing defining what function to call is evaluated, see Section 11.2.3 of the specification:

    11.2.3 Function Calls

    The production CallExpression : MemberExpression Arguments is evaluated as follows:

    1. Let ref be the result of evaluating MemberExpression.
    2. Let func be GetValue(ref).
    3. Let argList be the result of evaluating Arguments, producing an internal list of argument values (see 11.2.4).
    4. If Type(func) is not Object, throw a TypeError exception.
    5. If IsCallable(func) is false, throw a TypeError exception.
    6. If Type(ref) is Reference, then

        a) If IsPropertyReference(ref) is true, then

            i. Let thisValue be GetBase(ref).

        b) Else, the base of ref is an Environment Record

            i. Let thisValue be the result of calling the ImplicitThisValue concrete method of GetBase(ref).
    7. Else, Type(ref) is not Reference.

      a) Let thisValue be undefined.
    8. Return the result of calling the [[Call]] internal method on func, providing thisValue as the this value and providing the list argList as the argument values.

    Steps 1 and 2 occur before the function is redefined.

    The solution is of course to make things happen in the order you expect (live example | source):

    window.a = function(x){ 
        var r = x*2; 
        window.a =alert; // redefines itself after first call
        return r;
    }; 
    var val = a(2);
    a('2 * 2 = '+ val);
    

    Side note: It’s interesting that your first example works in Chrome (V8) (it also works in IE6’s version of JScript; but then, JScript had lots of issues). It shouldn’t work, and doesn’t in Firefox (SpiderMonkey), Opera (Carakan), or IE9 (Chakra).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

consider the following code: <script> function testFunc(){ alert('test') } $(function(){ var g = document.getElementById
Consider the following code: $(window).unload(function () { console.log('foo'); }); If I trigger the unload
Consider this code: function Foo() { } Foo.prototype.alert = function() { alert(this); } (new
Consider this: window.onload = function () { myObj.init(); }; var myObj = { init:
Consider this piece of code var crazy = function() { console.log(this); console.log(this.isCrazy); // wrong.
Consider the following code: hashString = window.location.hash.substring(1); alert('Hash String = '+hashString); When run with
Consider this JavaScript code... (function() { window.myNameSpace_callback = function() { // Do whatever. delete
Consider the following top-level javascript code: if (this.window === window) alert('same'); else alert('different'); //
Consider this code: public <T> List<T> meth(List<?> type) { System.out.println(type); // 1 return new
This is rather interesting, I think. Consider following code, both the window.onload and body

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.