简体   繁体   English

所有函数都在IIFE表达式内吗?

[英]Are all functions inside of an IIFE expressions?

If (function foo(){}) is an expression due to the 'context' as "(Parenthesis)" are a grouping operator and grouping operator can only contain an expression. 如果(function foo(){})是一个表达式,由于“上下文”为“(Parenthesis)”,则它是分组运算符,而分组运算符只能包含一个表达式。

Which leads to the question, can you declare a function inside of an IIFE or it would still count as an function expression? 这就引出了一个问题,您可以在IIFE内声明一个函数,还是仍可以算作一个函数表达式?

The function itself becomes an expression if it is wrapped in parenthesis, because the parenthesis create an expression context. 如果将函数包装在括号中,则函数本身将成为表达式,因为括号会创建表达式上下文。

This does not effect any of the statements or declarations in the function body whatsoever. 这不会影响函数体中的任何语句或声明。

(function iife() {
     function example() {
          …
     }
     …
}());

Here, the literal for iife is an expression because it's inside (…()) , but example is a declaration as usual because it's part of a function body. 在这里, iife的文字是一个表达式,因为它在(…())内部,而example是照例声明,因为它是函数体的一部分。

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

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