0

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?

1
  • 1
    It's part of anonymous function. You get self-executing anonymous function in (function)();, but later assign it's value to alpha Commented Nov 28, 2017 at 8:10

4 Answers 4

5

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

Sign up to request clarification or add additional context in comments.

1 Comment

var alpha = function(){}; this is still anonymous function. IsAnonymousFunctionDefinition takes expression as its argument. In this case, the expression is funcion(){};. And it returns true.
2

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.

Comments

2

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.

Comments

1

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().

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.