Nested function X vs. nested varX = function

  • Thread starter Richard A. DeVenezia
  • Start date
R

Richard A. DeVenezia

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' ?
 
C

Code Ronin

Richard A. DeVenezia said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top