Nested function X vs. nested varX = function

Discussion in 'Javascript' started by Richard A. DeVenezia, Sep 8, 2003.

  1. These seem to do the same thing, beyond the source code differences, are
    there any differences ?

    function foo () {
    y = bar(2)
    return
    function bar (x) {return x*x}
    }

    function foo () {
    var bar = function(x) { return x*x }
    y = bar(2)
    return
    }

    I recall Lasse mentioning a one-pass. Now I see it, In the first foo the
    bar function is available prior to it's definition because of the one-pass.
    In the second foo, the function var bar has to be assigned before it can be
    used.

    Where is a good reference discussing the 'one-pass' or 'first-pass' ?

    --
    Richard A. DeVenezia
     
    Richard A. DeVenezia, Sep 8, 2003
    #1
    1. Advertising

  2. Richard A. DeVenezia

    Code Ronin Guest

    "Richard A. DeVenezia" <> wrote in message news:<bji3j2$jjdus$-berlin.de>...

    > Where is a good reference discussing the 'one-pass' or 'first-pass' ?


    The ECMA-262 standards, although it will not use that terminology. See
    the section on execution contexts.

    > These seem to do the same thing, beyond the source code differences, are
    > there any differences ?


    I believe they are so minimal as to be irrelevant (unless someone sees
    something I do not). But here is one. Change the "return" statements
    to "return bar.toString()" and compare the results. The first is a
    named function object; the second is an anonymous function object
    assigned to a variable.

    At first I was unsure of the intent of your first function's return.
    Is it "return;" or "return function bar (x) {return x*x};"? I figured
    it was the former, otherwise your question would not make sense.

    If you use a code cruncher, the lack of ";" will bite you someday.
     
    Code Ronin, Sep 9, 2003
    #2
    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. Russ Perry Jr
    Replies:
    2
    Views:
    4,189
    Russ Perry Jr
    Aug 20, 2004
  2. Chad E. Dollins
    Replies:
    3
    Views:
    668
    Kai-Uwe Bux
    Nov 8, 2005
  3. request@no_spam.com
    Replies:
    5
    Views:
    444
  4. Ultrus
    Replies:
    3
    Views:
    390
    Stefan Behnel
    Jul 9, 2007
  5. Mohamad Khaleghy
    Replies:
    1
    Views:
    105
    Joerg Meier
    May 23, 2014
Loading...

Share This Page