1

The following line:

var A = function def() {alert();};

Only A() invokes the function. def() does not. Why is it so? Isn't the left side a function delaration?

Boyang
  • 2,520
  • 5
  • 31
  • 49

1 Answers1

0

In the function expression the function name is primarily used for self-calling. IRL this feature is handy in case of anonymous functions and recursive calls, i.e.

(function def() {
    // ...
    def();
})();
VisioN
  • 143,310
  • 32
  • 282
  • 281
  • Ok make sense. But why def() has no effect from outside in the question while it would have had if it were defined using "function def() {}"? – Boyang May 22 '13 at 07:50
  • 1
    @CharlesW. This is because the name of function expression is not visible outside of it's scope. – VisioN May 22 '13 at 07:53
  • Okay thanks! And is that also the reason why we can have many anonymous functions? Cause they are confined in their scopes so there's no conflict or ambiguity? – Boyang May 22 '13 at 07:57