我想知道这是什么意思:

(function () {

})();

这是在说document。onload吗?


当前回答

Normally, JavaScript code has global scope in the application. When we declare global variable in it, there is a chance for using the same duplicate variable in some other area of the development for some other purpose. Because of this duplication there may happen some error. So we can avoid this global variables by using immediately invoking function expression , this expression is self-executing expression.When we make our code inside this IIFE expression global variable will be like local scope and local variable.

我们可以通过两种方式创建IIFE

(function () {
    "use strict";
    var app = angular.module("myModule", []);
}());

OR

(function () {
    "use strict";
    var app = angular.module("myModule", []);
})();

在上面的代码片段中,“var app”现在是一个局部变量。

其他回答

立即调用的函数表达式(IIFE)立即调用函数。这仅仅意味着函数在定义完成后立即执行。

更常见的三个词:

// Crockford's preference - parens on the inside
(function() {
  console.log('Welcome to the Internet. Please follow me.');
}());

//The OPs example, parentheses on the outside
(function() {
  console.log('Welcome to the Internet. Please follow me.');
})();

//Using the exclamation mark operator
//https://stackoverflow.com/a/5654929/1175496
!function() {
  console.log('Welcome to the Internet. Please follow me.');
}();

如果对它的返回值没有特殊要求,那么我们可以这样写:

!function(){}();  // => true
~function(){}(); // => -1
+function(){}(); // => NaN
-function(){}();  // => NaN

或者,它可以是:

~(function(){})();
void function(){}();
true && function(){ /* code */ }();
15.0, function(){ /* code */ }();

你甚至可以这样写:

new function(){ /* code */ }
31.new function(){ /* code */ }() //If no parameters, the last () is not required

它只是一个匿名函数,在创建后立即执行。

这就像你把它赋值给一个变量,然后在后面使用它,只是没有变量:

var f = function () {
};
f();

在jQuery中,有一个类似的构造,你可能会想到:

$(function(){
});

这是绑定就绪事件的简写形式:

$(document).ready(function(){
});

但是上面的两个结构都不是iife。

Normally, JavaScript code has global scope in the application. When we declare global variable in it, there is a chance for using the same duplicate variable in some other area of the development for some other purpose. Because of this duplication there may happen some error. So we can avoid this global variables by using immediately invoking function expression , this expression is self-executing expression.When we make our code inside this IIFE expression global variable will be like local scope and local variable.

我们可以通过两种方式创建IIFE

(function () {
    "use strict";
    var app = angular.module("myModule", []);
}());

OR

(function () {
    "use strict";
    var app = angular.module("myModule", []);
})();

在上面的代码片段中,“var app”现在是一个局部变量。

这是Javascript中立即调用的函数表达式:

为了理解JS中的IIFE,让我们分解一下:

表达式:返回值的东西 示例:在chrome控制台中尝试以下操作。这些是JS中的表达式。

A = 10 输出= 10 (1 + 3) 输出= 4

函数表达式: 例子:

函数表达式 var greet = function(name){ 返回“Namaste” + ' ' + 名称; } 问候(“桑托什”);

函数表达式如何工作: - JS引擎第一次运行时(执行上下文-创建阶段),这个函数(上面=右侧)不会被执行或存储在内存中。变量'greet'被JS引擎赋值为'undefined'。 -在执行期间(执行上下文-执行阶段),函数对象是动态创建的(尚未执行),被分配给'greet'变量,可以使用'greet(' someename ')'调用。

3.立即调用的函数表达式:

例子:

// IIFE
var greeting = function(name) {
    return 'Namaste' + ' ' + name;
}('Santosh')

console.log(greeting)  // Namaste Santosh. 

IIFE如何运作: -注意函数声明后面的'()'。每个函数对象都有一个可调用的'CODE'属性。我们可以使用大括号'()'调用它。 -所以这里,在执行期间(执行上下文-执行阶段),函数对象被创建并同时执行 -所以现在,greeting变量没有函数对象,而是有它的返回值(一个字符串)

JS中IIFE的典型用例:

下面的IIFE模式是非常常用的。

// IIFE 
// Spelling of Function was not correct , result into error
(function (name) {
   var greeting = 'Namaste';
   console.log(greeting + ' ' + name);
})('Santosh');

我们在这里做两件事。 a)将函数表达式包装在大括号()中。这将告诉语法解析器放在()中的whatever是一个表达式(在本例中是函数表达式),并且是一个有效的代码。 b)我们同时调用这个函数,在它的末尾使用()。

因此,这个函数同时被创建和执行(IIFE)。

IIFE的重要用例:

IIFE使我们的代码安全。 -作为一个函数,IIFE有自己的执行上下文,这意味着在它内部创建的所有变量都是这个函数的局部变量,不与全局执行上下文共享。

假设我的应用程序中使用了另一个JS文件(test1.js)和life . JS(见下文)。

// test1.js

var greeting = 'Hello';

// iife.js
// Spelling of Function was not correct , result into error
(function (name) { 
   var greeting = 'Namaste';
   console.log(greeting + ' ' + name);
})('Santosh');

console.log(greeting)   // No collision happens here. It prints 'Hello'.

所以IIFE帮助我们编写安全的代码,这样我们就不会无意中与全局对象发生冲突。

IIFE(立即调用函数表达式)是一个在脚本加载和退出时立即执行的函数。

考虑下面写在life .js文件中的函数

(function(){
       console.log("Hello Stackoverflow!");
   })();

上面的代码将在你加载life .js时立即执行,并打印'Hello Stackoverflow!在开发人员工具控制台。

有关详细解释,请参阅立即调用函数表达式(IIFE)