function redefinition and the standard

Discussion in 'Javascript' started by lallous, Jan 26, 2004.

  1. lallous

    lallous Guest

    Hello,

    In IE, when you redefine a function it will be overwriten by the latest
    declaration.

    Is that by the standard or by this browser implementation?

    --
    Elias
     
    lallous, Jan 26, 2004
    #1
    1. Advertising

  2. lallous

    Grant Wagner Guest

    lallous wrote:

    > Hello,
    >
    > In IE, when you redefine a function it will be overwriten by the latest
    > declaration.
    >
    > Is that by the standard or by this browser implementation?
    >
    > --
    > Elias


    function abc() { alert('hi'); }
    function abc() { alert('bye'); }
    abc();

    is equivilent to

    var abc = new Function("alert('hi');");
    var abc = new Function("alert('bye');");
    abc();

    so unless a browser is doing something interesting parsing the included
    JavaScript, it will always execute the last function defined with a
    particular name.

    --
    | Grant Wagner <>

    * Client-side Javascript and Netscape 4 DOM Reference available at:
    *
    http://devedge.netscape.com/library/manuals/2000/javascript/1.3/reference/frames.html

    * Internet Explorer DOM Reference available at:
    *
    http://msdn.microsoft.com/workshop/author/dhtml/reference/dhtml_reference_entry.asp

    * Netscape 6/7 DOM Reference available at:
    * http://www.mozilla.org/docs/dom/domref/
    * Tips for upgrading JavaScript for Netscape 7 / Mozilla
    * http://www.mozilla.org/docs/web-developer/upgrade_2.html
     
    Grant Wagner, Jan 27, 2004
    #2
    1. Advertising

  3. Grant Wagner <> writes:

    > function abc() { alert('hi'); }
    > function abc() { alert('bye'); }
    > abc();
    >
    > is equivilent to
    >
    > var abc = new Function("alert('hi');");
    > var abc = new Function("alert('bye');");
    > abc();


    While the conclusion is correct, this statement is slightly incorrect.
    (I.e., ignore this unless you are a pedant like me :)

    When Javascript interprets a block of code, it first treats all the
    function declarations in the order they appear, i.e., it creates the
    local variable and assigns their value. Then it treats all the
    variable declarations and declares the variables (i.e., creates them
    as properties of the variables object if they don't exist already),
    but does not assign a new value, even if the declaration looks like
    "var foo=42;". That is treated like "var foo;foo = 42;".
    Then it executes the code, including the assignments to variables.

    So

    var foo = 42;
    function foo(){}
    alert(foo);

    will alert "42".

    In any case, declaring a function or variable will create it as a
    local variable, a property of the variable object of the
    scope. Creating it twice is allowed, and the last assignment to it
    will win (functions first, in the order they appear, then assignments
    in the normal code, in the order the are executed).


    /L
    --
    Lasse Reichstein Nielsen -
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
    'Faith without judgement merely degrades the spirit divine.'
     
    Lasse Reichstein Nielsen, Jan 27, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jabba Laci

    avoid the redefinition of a function

    Jabba Laci, Sep 12, 2012, in forum: Python
    Replies:
    2
    Views:
    183
    Ramchandra Apte
    Sep 12, 2012
  2. D'Arcy Cain
    Replies:
    0
    Views:
    177
    D'Arcy Cain
    Sep 12, 2012
  3. Michael Torrie

    Re: avoid the redefinition of a function

    Michael Torrie, Sep 12, 2012, in forum: Python
    Replies:
    0
    Views:
    176
    Michael Torrie
    Sep 12, 2012
  4. Jabba Laci
    Replies:
    3
    Views:
    196
    Peter Otten
    Sep 13, 2012
  5. Tim Chase
    Replies:
    0
    Views:
    167
    Tim Chase
    Sep 12, 2012
Loading...

Share This Page