Reputation: 8240
Can someone clarify me out if the following code snippet represents anonymous function or Not?
var alpha = (function(){
/*
private space
*/
return{
//Some Code Here ...
}
})();
Is this an anonymous function? This looks like structure of anonymous function to me, but I read that anonymous function is one which has no name. Here I think alpha(variable) is the name assigned to the function, so that contradicts the concept.
I know if it would have been:
(function(){
return{
//Some Code Here ...
}
})();
Then this would have been Anonymous Function(self invoking) or IIFE.
Also, following is a simple function but Not Anonymous, because beta is assigned to the function (like my example above). So, if this is not anonymous function (as beta is pointed to function & represents it), then how can my previous function (alpha pointing to function) can be anonymous? Also, self invoking is extra part. Just because a function is self invoking doesn't make it Anonymous I believe.
var beta = function(){
//Some code
}
Can someone clarify me out?
Upvotes: 0
Views: 1274
Reputation: 6366
In example 1, the variable becomes a reference to the return value of the IIFE, whereas in example 3 it becomes a reference to the function itself.
You cannot call alpha()
(unless of course the IIFE returns a new function), but you can call beta()
.
Upvotes: 1
Reputation: 68393
Function
has a property called name
and spec clearly says
The value of the name property is an String that is descriptive of the function. The name has no semantic significance but is typically a variable or property name that is used to refer to the function at its point of definition in ECMAScript code.
Also, spec says
The abstract operation IsAnonymousFunctionDefinition determines if its argument is a function definition that does not bind a name.
Some samples below
var alpha = function(){};
console.log(alpha.name); //returns alpha
alpha = function abc(){};
console.log(alpha.name); //returns abc
alpha = { a : function(){} };
console.log(alpha.a.name); //returns a overrides alpha
alpha = (function(){
return function(){}
})();
console.log(alpha.name); //return "" since inner function doesn't have a name
alpha = (function(){
return (a = function(){})
})();
console.log(alpha.name); //return a since inner function is assigned to property a
alpha = (function(){
return function a (){}
})();
console.log(alpha.name); //return a
alpha = (function(){
return{
}
})();
console.log(alpha.name); //undefined since return value is an object
Upvotes: 5
Reputation: 8126
All functions in your example are anonymous. You can have an anonymous function assigned to a variable, but the function itself will still be anonymous. You can read a bit of explanation on wikibooks.
Upvotes: 2
Reputation: 2897
The top snippet contains an anonymous function, which is immediately called. The result of the function call (the object including Some Code here
) is assigned to alpha
, so not the function itself.
The beta function is also anonymous (it has no name), but it is assigned to beta
.. You could also assign it to gamma
(as in gamma = beta
), but the function would still be anonymous - these are just references to it, not its name.
Maybe this will make it clearer - you can say
var bubu = function mimi() {
}
This function is not anonymous, its name is mimi
, and it is assigned to a variable called bubu
. But bubu
is still not the function's name, it remains mimi
.
Upvotes: 2