Are function names stored inside IIFE?

As I understand it, the grouping operator around a function declaration causes the function to be evaluated as an expression. This is what allows the execution parenthesis operator ()

to work. However, this approach removes the function name from being accessible from the outside. I'm wondering how the IIFE function names are stored versus the function declaration names that are available in the scope they are declared in.

(function hidden(){
    console.log("function executed");
})()

      

+2


source to share


5 answers


There's a good article by Angus Kroll on the difference between function declaration and function expression in javascript ( https://javascriptweblog.wordpress.com/2010/07/06/function-declarations-vs-function-expressions/ )

Basically what you are trying to do is treat the hidden () function as a function declaration inside a function expression. As Croll points out in the article, inside a function expression "the name of the function (if any)) is not visible outside of its scope (unlike function declarations)".

If you are rewriting the anonymous function as assignment, then it is clearer:

var a = function hidden() {
  console.log('inside hidden');
}

      

now it will be an error:

var a = function hidden() {
  console.log('inside hidden');
}
hidden();

      



because the function name is not available outside of its context.

This will work fine:

var a = function hidden() {
  console.log('inside hidden');
}
a();

      

since the variable can be referenced outside of its own context, as you would expect (otherwise it would not be available anywhere except inside the body of the hidden () function).

If we look at the anonymous version again, you can see why it fails:

(function hidden() {
  console.log('inside hidden');
}
// We're outside of IIFEs function body here
// so NO code can be executed.
)();

      

+1


source


Because it's just a syntax error, as you said, "unexpected identifier". You can fix this by using the comma operator:, (function hidden() { ... }, hidden())

but the name hidden

(holding the function object) from your function expression will only be available inside the function scope. You can solve this by executing the definition:

(function() {
    function hidden(){
        console.log("hidden");
    }
    hidden();
})();

      

You can also execute related definitions. How...

ES4 ( reference interpreter builds):



{
    let function hidden()
    {
        console.log("hidden");
    }
    hidden();
}

      

JavaScript based on ES6

{
    let hidden = function()
    {
        console.log("hidden");
    }
    hidden();
}

      

0


source


The function name is accessible through accessing the function name property:

IIFE:

(function hidden() {
    console.log("Function name is: '" + hidden.name + "'"); //Function name is: 'hidden'
})();
console.log(hidden.name);//Error because IIFEs have their own private scope. 

      

function declaration:

function available(){
    return;
}
console.log(available.name);//"Available" - the console.log has access to the function scope.

      

0


source


On the other hand, the following is perfectly true:

(two = function() {
  console.log('two');
}, 
console.log('one'), 
two)()
      

Run codeHide result


-1


source


You have a syntax error because

(function hidden(){
    console.log("hidden");
}
hidden();)

      

is not an expression, but

(function hidden(){
    console.log("hidden");
})

      

is an expression that the function itself returns, and you call it with ()

.

You can do this with an expression by adding a comma between the two parts and removing the semi-colon. However, hidden

will be defined only within the function itself, and you cannot name it. This is called the named function expression

// Uncaught ReferenceError: hidden is not defined
(function hidden(){
    console.log("hidden");
},
hidden())

      

You should probably do the following

(function() {
    function hidden(){
        console.log("hidden");
    }
    hidden();
})()

      

In this case, you create a function declaration that appears inside the containing IIFE

-1


source







All Articles