Differences between myFn=function(a) and function myFn(a) as method

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

    #1

    Differences between myFn=function(a) and function myFn(a) as method


    // Between these two ways of creating a JS function, do they represent the
    exact same thing?:
    function myFn(a) {return 1;} // SAMPLE A
    myFn = function(a) {return 1;} // SAMPLE B

    // ... and then I assign one or the other function as a method like so:
    function myObject() {
    this.f = myFn;
    }
    // Base on the myObject, when I create new instances does it matter where
    SAMPLE A or B was used above?
    var o1 = new myObject();
    var o2 = new myObject(); //etc...

    /* For example, I might assume that using SAMPLE A results in one function
    entry point being used amongst the multiple methods I created.
    Would SAMPLE B end up creating copies of the function if it were used?
    */

    Is there any good supporting JavaScript documentation that gets into the
    nitty gritty of this question?
    Thanks.
    -- John


  • Lasse Reichstein Nielsen

    #2
    Re: Differences between myFn=function(a ) and function myFn(a) asmethod

    "John" <NoSpam-johnsnews1176@h otmail.com> writes:
    [color=blue]
    > // Between these two ways of creating a JS function, do they represent the
    > exact same thing?:
    > function myFn(a) {return 1;} // SAMPLE A
    > myFn = function(a) {return 1;} // SAMPLE B[/color]

    Almost (I assume this is in the global scope, or that myFn is declared
    as a variable). The difference is so small that you would probably not
    notice it. If used inside a function body, the function is defined before
    the code of the body is executed, while the variable is only assigned
    when the assignment is executed.

    Try these:
    ---
    function test1() {
    alert(myFn()); // alerts 1
    function myFn(){return 1;};
    }
    ---
    vs
    ---
    function teste() {
    alert(myFn()); // gives "myFn is not a function" error
    var myFn = function(){retu rn 1;};
    }
    ---

    If this difference ever matters, you should reconsider how you write
    your code :)

    [color=blue]
    > // ... and then I assign one or the other function as a method like so:
    > function myObject() {
    > this.f = myFn;
    > }[/color]

    They are completely equivalent as functions.
    [color=blue]
    > // Base on the myObject, when I create new instances does it matter where
    > SAMPLE A or B was used above?[/color]

    No.
    [color=blue]
    > /* For example, I might assume that using SAMPLE A results in one function
    > entry point being used amongst the multiple methods I created.
    > Would SAMPLE B end up creating copies of the function if it were used?
    > */[/color]

    Whether you write
    ---
    function myFn() {...}
    function myObject() {
    this.f = myFn;
    }
    ---
    or
    ---
    var myFn = function() {...};
    function myObject() {
    this.f = myFn;
    }
    ---
    only one function is created for myFn.

    Whether you write
    ---
    function myObject() {
    function myFn() {...};
    this.f = myFn;
    }
    ---
    or
    ---
    function myObject() {
    var myFn = function() {...};
    this.f = myFn;
    }
    ---
    one functions is created per call to myObject.

    However, if you write:
    ---
    for(var i=0;i<10;i++) {
    function myFn(){...}
    f[i]=myFn;
    }
    ---
    then only one function is created, while
    ---
    for(var i=0;i<10;i++) {
    var myFn = function(){...}
    f[i]=myFn;
    }
    ---
    creates a new function for each time through the loop (although
    ECMAScript implementations *may* optimize it and reuse the function).
    [color=blue]
    > Is there any good supporting JavaScript documentation that gets into the
    > nitty gritty of this question?[/color]

    The ECMA262 v3 standard would be the definitive guide to nitty gritty
    details:
    <URL: http://www.mozilla.org/js/language/E262-3.pdf >

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