简体   繁体   中英

Anonymous Function with variable name assigned in JavaScript

Can someone clarify me out if the following code snippet represents anonymous function or ?

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 (self invoking) or . (自我调用)或

Also, following is a simple function but Anonymous, because beta is assigned to the function (like my example above). 匿名的,因为beta是分配给该函数的(就像我上面的示例一样)。 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 I believe.

var beta = function(){
    //Some code 
}

Can someone clarify me out?

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 

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 .

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 .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM