function redefinition and the standard

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • lallous

    function redefinition and the standard

    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


  • Grant Wagner

    #2
    Re: function redefinition and the standard

    lallous wrote:
    [color=blue]
    > 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[/color]

    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 <gwagner@agrico reunited.com>

    * Client-side Javascript and Netscape 4 DOM Reference available at:
    *


    * Internet Explorer DOM Reference available at:
    *
    Gain technical skills through documentation and training, earn certifications and connect with the community


    * 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-deve...upgrade_2.html


    Comment

    • Lasse Reichstein Nielsen

      #3
      Re: function redefinition and the standard

      Grant Wagner <gwagner@agrico reunited.com> writes:
      [color=blue]
      > 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();[/color]

      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 - lrn@hotpop.com
      DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
      'Faith without judgement merely degrades the spirit divine.'

      Comment

      Working...