To expand on @Amir's answer:
js>(function bar() {})(3)
js>bar
console:1 ReferenceError: bar is not defined
js>function foo() {}
js>foo
function foo() {
}
(code executed in jsdb)
These are named functions, and if you don't put parentheses around the function definition, they become part of the local scope. function foo() {}
becomes available for use later, but bar
does not.
As a third example:
var x = function baz() {};
If you run this:
js>var x = function baz() {}
js>baz
console:1 ReferenceError: baz is not defined
You'll note that it's the similar case as (function baz(){})(3)
.
The case of
function foo() {}
is special, the Javascript interpreter sees that form and says, "Oh, you're trying to define a function named "foo" in the local scope."
As for why a named function is useful even if it doesn't get defined in the local scope -- the named function is visible from the scope of the function itself:
js>var x = function fact(n) { return n*((n < 2) ? 1 : fact(n-1)); }
js>x(3)
6
js>fact
console:1 ReferenceError: fact is not defined
Here we have a factorial function named "fact", but the name "fact" is only visible inside the scope of the function itself.