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?
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?
-
1
var func=function F(){};
will makeF()
available inside function block, but not outside. – Passerby Commented Jul 1, 2013 at 3:07 -
arguments.callee
is deprecated, so use named function expressions if you need that kind of functionality. – holographic-principle Commented Jul 1, 2013 at 3:42
3 Answers
Reset to default 7The 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
The name
in a function literal is optional, if omitted as in the first case you show the function is said to be anonymous.
This is from JavaScript: The Good Parts by Douglas Crockford:
A function literal has four parts. The first part is the reserved word function. The optional second part is the function's name. The function can use its name to call itself recursively. The name can also be used by debuggers and development tools to identify the function. If a function is not given a name, as shown in the previous example, it is said to be anonymous.
The first function doesn't have a name.
Assigning a function to a variable doesn't give the function a name.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744395838a4572134.html
评论列表(0条)