Nested function X vs. nested varX = function

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Richard A. DeVenezia

    #1

    Nested function X vs. nested varX = function

    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


  • Code Ronin

    #2
    Re: Nested function X vs. nested varX = function

    "Richard A. DeVenezia" <radevenz@ix.ne tcom.com> wrote in message news:<bji3j2$jj dus$1@ID-168040.news.uni-berlin.de>...
    [color=blue]
    > Where is a good reference discussing the 'one-pass' or 'first-pass' ?[/color]

    The ECMA-262 standards, although it will not use that terminology. See
    the section on execution contexts.
    [color=blue]
    > These seem to do the same thing, beyond the source code differences, are
    > there any differences ?[/color]

    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.

    Comment

    Working...