2

那里的 JS 专家可以解释为什么会这样:

$$={}
(function(x){
    x.newModule = {
            func: function(){...}
    };
})($$);
$$.newModule.func()

优于这个?

$$.newModule = {
    func: function() {...}
}
$$.newModule.func()
4

2 回答 2

7

extra 函数为您提供了您可能想要使用的额外本地范围(尽管它不在您的简短示例中)。

(function(x){
    var privateFunction = function() {};
    var privateCounter = 1;
    x.newModule = {
            func: function(){...}
    };
})($$);
于 2012-07-25T03:29:16.880 回答
0

In the first pattern you can have private methods and variables which are not possible in the second pattern. That is the reason the first pattern is superior.

于 2012-07-25T03:31:26.487 回答