什么是自动执行的匿名函数,或者这个代码做了什么? [英] what is self-executing anonymous function or what is this code doing?

查看:116
本文介绍了什么是自动执行的匿名函数,或者这个代码做了什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  var module = {}; 


(function(exports){

exports.notGlobalFunction = function(){
console.log('I am not global');
};

}(module));

函数notGlobalFunction(){
console.log('I am global');
}

notGlobalFunction(); //输出我是全局的
module.notGlobalFunction(); //输出我不是全球的

任何人都可以帮我理解这里发生了什么?我知道如果你调用 notGlobalFunction(),它只会调用第二个函数。



但是 var module = {} 在做什么?为什么它会在第一个函数内再次调用?



它说这通常被称为自动执行的匿名函数,但我并不真正知道这意味着什么。

解决方案

立即调用的函数通常用于创建局部函数作用域,该作用域是私有的,不能从外部世界访问,定义它自己的本地符号而不影响外部世界。这通常是一种很好的做法,但在这种特殊情况下,我不认为它会产生任何好处,而不是几行代码,因为它不用于任何事情。



这段代码:

 (function(exports){

exports.notGlobalFunction = function(){
console.log('I am not global');
};

}(module));

如果没有像这样的立即调用,那么这段代码就相同了:

  module.notGlobalFunction = function(){
console.log('I am not global');
};

有一点不同的是,第一个是的别名,模块称为 exports 被创建,它是立即调用的功能块的本地。但是,别名并没有什么独特之处,代码也可以直接使用 modules






变量 modules 被创建为一个单一的全局父对象,然后可以将许多其他全局变量保存为属性。这通常被称为命名空间。这通常是一个很好的设计模式,因为它最大限度地减少了可能与同一项目/页面中使用的其他代码段冲突的顶级全局变量的数量。



而不是像下面这样创建多个顶级变量:

  var x,y,z; 

可以像这样创建一个顶级变量:

  var modules = {}; 

然后将所有其他全局变量作为属性:

  modules.x = 5; 
modules.y = 10;
modules.z = 0;

这样,尽管仍有多个全局变量,但只有一个顶级全局变量与其他代码段冲突。




类似地,立即调用的函数创建一个本地专用作用域,其中可以创建变量它们是本地的,并且不会干扰其他代码片段:

 (function(){
var x ,y,z;

//变量x,y和z可用于此即时调用函数
中的任何代码,并且它们在此函数块内充当全局变量,
//在程序的生命周期中值将持续
//但是,它们不是真正的全局变量,并且不会干扰任何其他全局
//变量,并且不能由此外的代码访问块。
//他们创建隐私和隔离,但同样可以工作


})();

将参数传递给立即调用的函数只是将值传递给立即调用的函数它有自己的本地符号:

 (function(exports){
//在本地创建一个符号这个功能块称为exports
//,它被分配了模块
的初始值)(module);


var module = {};


(function(exports){

  exports.notGlobalFunction = function() {
    console.log('I am not global');
  };  

}(module));

function notGlobalFunction() {
  console.log('I am global');
}

notGlobalFunction(); //outputs "I am global"
module.notGlobalFunction(); //outputs "I am not global"

Can anyone help me understand what's going on here? I get that if you call notGlobalFunction(), it will just call the second function.

But what is var module = {} doing? and why is it called again inside the first function?

It says this is commonly known as a self-executing anonymous function but I don't really know what that means.

解决方案

Immediately invoked functions are typically used to create a local function scope that is private and cannot be accessed from the outside world and can define it's own local symbols without affecting the outside world. It's often a good practice, but in this particular case, I don't see that it creates any benefit other than a few more lines of code because it isn't used for anything.

This piece of code:

(function(exports){

  exports.notGlobalFunction = function() {
    console.log('I am not global');
  };  

}(module));

Would be identical to a piece of code without the immediate invocation like this:

module.notGlobalFunction = function() {
   console.log('I am not global');
};  

The one thing that is different is that in the first, an alias for modules called exports is created which is local to the immediately invoked function block. But, then nothing unique is done with the alias and the code could just as well have used modules directly.


The variable modules is created to be a single global parent object that can then hold many other global variables as properties. This is often called a "namespace". This is generally a good design pattern because it minimizes the number of top-level global variables that might conflict with other pieces of code used in the same project/page.

So rather than make multiple top level variables like this:

var x, y, z;

One could make a single top level variable like this:

var modules = {};

And, then attach all the other globals to it as properties:

modules.x = 5;
modules.y = 10;
modules.z = 0;

This way, while there are still multiple global variables, there is only one top-level global that might conflict with other pieces of code.


Similarly, an immediately invoked function creates a local, private scope where variables can be created that are local to that scope and cannot interfere with other pieces of code:

(function() {
    var x, y, z;

    // variables x, y and z are available to any code inside this immediately invoked function
    // and they act like global variables inside this function block and
    // there values will persist for the lifetime of the program
    // But, they are not truly global and will not interfere with any other global
    // variables and cannot be accessed by code outside this block.
    // They create both privacy and isolation, yet work just as well


})();

Passing an argument into the immediately invoked function is just a way to pass a value into the immediately invoked function's scope that will have it's own local symbol:

(function(exports) {
    // creates a local symbol in this function block called exports
    // that is assigned an initial value of module
})(module);

这篇关于什么是自动执行的匿名函数,或者这个代码做了什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆