Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript: var myFunc = function() vs. var myFunc = function myFunc()

Tags:

javascript

What is the difference between these?

var myFunc = function() {
  // ...
};

vs.

var myFunc = function myFunc() {
  // ...
};

In the 2nd example, arguments.callee.caller.name works, but not in the first one. Is there anything wrong with the 2nd syntax?

like image 769
vcardillo Avatar asked Dec 13 '25 21:12

vcardillo


1 Answers

The second one has a name while the first one doesn't. Functions are objects that have a property name. If the function is anonymous then it has no name.

var a = function(){}; // anonymous function expression
a.name; //= empty

var a = function foo(){}; // named function expression
a.name; //= foo
like image 60
elclanrs Avatar answered Dec 15 '25 09:12

elclanrs