简体   繁体   English

立即调用 Function 中的表达式 javascript

[英]Immediately Invoked Function Expression in javascript

What is the reason of putting ( ) in the end for Immediately Invoked Function Expression in javascript javascript 中 Immediately Invoked Function Expression 将 ( ) 放在末尾的原因是什么

(function() {
    // Code that runs in your function
})( /* this parenthesis in the end */ )

An Immediately Invoked Function Expression is a:立即调用 Function 表达式是:

(function() {... })Function Expression which is Immediately Invoked() (function() {... })Function立即调用的表达式 → ()

To elaborate, (function() {... }) is merely defining the function, and a function which is defined but never called is doing absolutely nothing.详细来说, (function() {... })只是定义function,而定义但从未调用的 function 绝对什么都不做。

In order for the code in the function to be executed, it needs to be invoked (often referred to as calling the function).为了执行function中的代码,需要调用它(通常称为调用函数)。 That is why you wrap the function definition in parentheses (making it an expression that evaluates to a reference to the function) and then immediately invoke (or call) the function with any arguments () - or no arguments as in your sample.这就是为什么您将 function 定义括在括号中(使其成为一个表达式,计算出对函数的引用),然后立即调用(或调用)function 与任何 arguments () - 或没有 arguments 就像您的示例中一样。

It is more or less equivalent to doing this:它或多或少等同于这样做:

const someFunc = (function() { ... }); //merely assigns a function, the code inside doesn't run
someFunc(); //invokes the function (but in this case it isn't immediate)

except in this case you've bound the function reference to a variable and so it is no longer an IIFE.除了在这种情况下,您已将 function 引用绑定到一个变量,因此它不再是 IIFE。

The reason is purely syntactical.原因纯粹是语法上的。 The JavaScript parser has to be able to easily differentiate between function declarations and function expressions. JavaScript 解析器必须能够轻松区分 function 声明和 function 表达式。 If we leave out the parentheses around the function expression, and put our immediate call as a separate statement function(){}(3) , the JavaScript parser will start processing it, and will conclude, because it's a separate statement starting with the key- word function, that it's dealing with a function declaration.如果我们省略 function 表达式周围的括号,并将我们的立即调用作为单独的语句function(){}(3) ,JavaScript 解析器将开始处理它并结束,因为它是一个以键开头的单独语句- 单词 function,它正在处理 function 声明。

Because every function declaration has to have a name (and here we didn't specify one), an error will be thrown.因为每个 function 声明都必须有一个名称(这里我们没有指定),所以会抛出错误。 To avoid this, we place the function expression within parentheses, signaling to the JavaScript parser that it's dealing with an expression, and not a statement.为避免这种情况,我们将 function 表达式放在括号内,向 JavaScript 解析器发出信号,表明它正在处理表达式,而不是语句。 There's also an alternative way of achieving the same goal:还有一种实现相同目标的替代方法:

  (function(){}(3))

By wrapping the immediate function definition and call within parentheses, you can also notify the JavaScript parser that it's dealing with an expression.通过将立即的 function 定义和调用包装在括号内,您还可以通知 JavaScript 解析器它正在处理一个表达式。

Those four expressions are variations of the same theme of immediately invoked function expressions often found in various JavaScript libraries:这四个表达式是立即调用的 function 表达式的同一主题的变体,经常在各种 JavaScript 库中找到:

+function(){}();
-function(){}();
!function(){}();
~function(){}();

This time, instead of using parentheses around the function expressions to differentiate them from function declarations, we can use unary operators: +, -, , .这一次,我们可以使用一元运算符:+、-、、、,而不是在 function 表达式周围使用括号来将它们与 function 声明区分开来。 and ~.和~。 We do this to signal to the JavaScript engine that it's dealing with expressions and not statements.我们这样做是为了向 JavaScript 引擎发出信号,表明它正在处理表达式而不是语句。

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

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