简体   繁体   English

将全局变量传递给自动调用函数或“IIFE”的目的

[英]Purpose of passing global vars to self-invoking function or “IIFE”

I see a lot of this in older JavaScript 我在旧的JavaScript中看到了很多这样的东西

(function (w){

    w.bar = 'baz';

 })(window); 

What is the advantage of the above, over 上面有什么优点,结束了

(function(){

    window.bar = 'baz';

})(); 

same goes for any global variable , or variable defined outside the IIFE . 同样适用于任何global variable ,或在IIFE之外定义的IIFE

  1. Makes it explicit that you are using (and probably modifying) globals in the function. 明确表示您正在使用(并可能修改)函数中的全局变量。
  2. Allows you to modify the behavior in the future. 允许您以后修改行为。 Maybe you have a mockWindow for a unit test. 也许你有一个用于单元测试的mockWindow Maybe you are using Node.js and you don't have a window , but want to add to the globals var instead. 也许您正在使用Node.js并且您没有window ,但是想要添加到globals var。

ps IMO the trivial performance gain mentioned by @Rayon is a red herring. ps IMO @Rayon提到的微不足道的性能提升是一个红色的鲱鱼。

In practice there is not much (any?) difference wit the example you have given, but you have probably oversimplified it from the code you are actually looking at. 在实践中,你给出的例子没有太多(任何?)差异,但你可能从你实际看到的代码中过度简化了它。

In a more realistic program, you will have scopes and calls backs which are triggered and run in the eventloop asynchronously, and you are binding the variable to a specific instance in a closure -- so; 在一个更现实的程序中,您将具有异步触发和在事件循环中运行的作用域和回调,并且您将变量绑定到闭包中的特定实例 - 所以;

(function (w){
    setTimeout(function(){w.bar = 'baz';},100);
})(window); 
window = window2;

The bar is still being set in the original window, since that is what is bound to w - where in 该栏仍然在原始窗口中设置,因为这是必然的w - 在哪里

(function (){
    setTimeout(function(){window.bar = 'baz';},10);
})(window); 
window = window2;

It will be set in the instance window2, since that is how the window is bound when the execution of the code eventually happens. 它将在实例window2中设置,因为这是在最终发生代码执行时窗口的绑定方式。

In this example "window" is a global variable, but the same is the case regardless of the scope of the variable that is bound. 在此示例中,“window”是一个全局变量,但无论绑定的变量的范围如何,情况都是如此。

Passing window is done because local variables are easier and faster to access than global variables ...this may show slight difference in performance . 完成传递窗口是因为局部变量比全局变量更容易和更快地访问...这可能会在性能上略有不同。 The method really comes in handy with module patterns and/or dependency injection . 该方法确实派上了模块模式和/或依赖注入。

Example

var moduleFirst = (function(){
    var name = "harry";
    return {
        firstparam : name
    }
})();
var moduleTwo = (function(x){
    console.log(x.firstparam);
})(moduleFirst)

Output will be : harry 输出将是:哈里

So when window gets passed in IIFE ; 因此当IIFE传递窗口时; all of its revealed methods are available in a local variable . 所有显示的方法都可以在局部变量中使用。

By passing global objects like window, document, $ to IIFE(Immediately Invoked Function Expression) one can enhance the performance by reducing the scope lookup time. 通过将全局对象(如window,document,$)传递给IIFE(立即调用函数表达式),可以通过缩短范围查找时间来提高性能。 Remember Javascript looks for property at first in local scope and way up chaining till global scope. 记住Javascript首先在本地范围内查找属性,然后向上链接到全局范围。 So accessing window object at local scope reduce the lookup time. 因此,在本地范围访问窗口对象会减少查找时间。

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

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