简体   繁体   English

模块模式扩充

[英]Module pattern augmentation

From what I know, this is how you implement loose augmentation to modules: 据我所知,这是对模块实施宽松扩充的方式:

// file1.js:
var mod = (function(){

    return { foo: 1 }

})();

// file2.js:
var mod = (function(mod){

    console.log(mod); // Object { foo: 1 } 

})(mod || {});

This obviously works, but what I would like a couple of things straighten out: 这显然是可行的,但我想澄清两件事:

  1. Is it safe to say that declaring a variable with the same name in javascript will never cause an issue (even in strict mode)? 可以肯定地说,在javascript中声明具有相同名称的变量永远不会引起问题(即使在严格模式下)?
  2. Why is the mod argument in the second IIFE not overwritten by the second var mod ? 为什么第二个IIFE中的mod参数没有被第二个var mod覆盖? Are self executing functions executed before variables are declared? 是否在声明变量之前执行自执行函数?

http://jsfiddle.net/aVV9S/ http://jsfiddle.net/aVV9S/

Is it safe to say that declaring a variable with the same name in javascript will never cause an issue (even in strict mode)? 可以肯定地说,在javascript中声明具有相同名称的变量永远不会引起问题(即使在严格模式下)?

It depends where you declare that variable. 这取决于您在何处声明该变量。 If it is in the global scope, there is a possibility that your variable can be overwritten somewhere else if a variable of that same name is also defined in the global scope. 如果在全局范围内,则如果在全局范围内也定义了相同名称的变量,则可能会在其他地方覆盖您的变量。

Why is the mod argument in the second IIFE not overwritten by the second var mod? 为什么第二个IIFE中的mod参数没有被第二个var mod覆盖? Are self executing functions executed before variables are declared? 是否在声明变量之前执行自执行函数?

Order of evaluation is such that the function arguments are evaluated first, and then the function itself is evaluated and invoked. 求值顺序是这样的:首先对函数参数进行求值,然后对函数本身求值并调用。 It is only after that, that the return value is assigned to the outer mod . 只有在此之后,才将返回值分配给外部mod Your second example also appears to be mistyped; 您的第二个示例似乎也输入错误; it should probably be: 它可能应该是:

var mod = (function(mod){

    console.log(mod); // Object { foo: 1 } 

})(mod || {});

The mod inside the function is local to the scope of that function only. 函数内部的mod仅限于该函数的作用域。 However, realize that once you have invoked this function, mod is no-longer { foo: 1} , but undefined since you are not returning anything from the IIFE. 然而,意识到,一旦你调用这个函数, mod是不再视为{ foo: 1}undefined ,因为你没有返回从IIFE什么。

Regarding your comment, you can actually see the order of execution happening in the following snippet: 关于您的评论,您实际上可以在以下片段中看到执行的顺序:

var foo = {
    test: function() {
        console.log("hello");
        return 5;
    }
};

var bar = (function(val) {
    console.log("there")
    console.log("bar is", bar);
    return 5;
})(foo.test());
console.log("bar is", bar);

The console will show: 控制台将显示:

hello
there
bar is undefined
bar is 5

Which shows that the arguments to the IIFE are evaluated first, then the IIFE itself is evaluated and invoked, and finally the value 5 is assigned to bar . 这表明首先评估IIFE的参数,然后评估并调用IIFE本身,最后将值5分配给bar

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

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