简体   繁体   English

函数名称是否存储在IIFE中?

[英]Are function names stored inside of an IIFE?

It is my understanding that the grouping operator around a function declaration forces the function to be evaluated as an expression. 据我了解,围绕函数声明的分组运算符会强制将函数作为表达式进行评估。 This is what allows the execution parentheses operator () to work. 这就是执行括号运算符()起作用的原因。 However, this approach removes the function name from being accessible outside itself. 但是,这种方法使函数名称无法从外部访问。 I'm wondering how IIFE function names are stored compared to function declaration names which are available in the scope they are declared. 我想知道与在声明范围内可用的函数声明名称相比,IIFE函数名称如何存储。

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

There's a good article by Angus Croll about the difference between a function declaration and a function expression in javascript ( https://javascriptweblog.wordpress.com/2010/07/06/function-declarations-vs-function-expressions/ ) Angus Croll撰写了一篇很好的文章,介绍了javascript中的函数声明和函数表达式之间的区别( https://javascriptweblog.wordpress.com/2010/07/06/function-declarations-vs-function-expressions/

Basically what you're trying to do is treat the function hidden() as a function declaration inside of a function expression. 基本上,您想要做的是将函数hidden()视为函数表达式的函数声明。 As Croll states in the article, inside a function expression, "the function name (if any) is not visible outside of it's scope (contrast with Function Declarations)." 正如Croll在文章中所说,在函数表达式内,“函数名(如果有的话)在其作用域之外是不可见的(与函数声明相反)。”

If you rewrite the anonymous function as an assignment it makes the point clearer I think: 如果您将匿名函数重写为赋值函数,那么我认为这很清楚:

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

now this will error: 现在这将错误:

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

because the name of the function is not available outside of its own context. 因为该函数的名称在其自身上下文之外不可用。

This however 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 wouldn't be reachable anywhere but inside the hidden() function body). 因为可以像您期望的那样在变量自身的上下文外部引用该变量(否则,除了hidden()函数体内,该变量将无法访问)。

If we take a look again at the anonymous version 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.
)();

Because it's simply a parsing/syntax error, as you said, "unexpected identifier" . 正如您所说的,这只是一个解析/语法错误,即“意外标识符” You could fix this by using the comma operator: (function hidden() { ... }, hidden()) , but the hidden name (holding the function object) from your function expression will be only available inside the function scope. 您可以使用逗号运算符来解决此问题: (function hidden() { ... }, hidden()) ,但是函数表达式中的hidden名称(包含函数对象)仅在函数作用域内可用。 You could solve this by doing a definition instead: 您可以通过定义来解决此问题:

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

You could also do bound definitions. 您还可以进行绑定定义。 Like... 喜欢...

ES4 ( reference interpreter builds): ES4( 参考解释器构建):

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

ES6-based JavaScript 基于ES6的JavaScript

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

The name of a function is available by accessing the function's name property: 通过访问函数的name属性可以使用函数的名称:

IIFE: 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's scope.

On the other hand the following is perfectly valid: 另一方面,以下内容完全正确:

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

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 returns the function itself and you call it with () . 是一个返回函数本身的表达式,您可以使用()调用。

You could make it an expression by adding a comma between the two parts, and removing the semi-colon. 您可以通过在两个部分之间添加逗号并删除分号来使其成为表达式。 However, hidden would be only defined within the function itself and you couldn't call it. 但是, hidden只能在函数本身中定义,而您不能调用它。 That's called a named function expression 这称为命名函数表达式

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

You probably meant to do the following 您可能打算执行以下操作

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

In that case, you are creating a function declaration that is visible within the containing IIFE 在这种情况下,您将创建一个在包含的IIFE中可见的函数声明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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