简体   繁体   English

以下JS语法有什么区别?

[英]What is the difference in the following JS syntax?

Following are two ways to define BW.Timer. 以下是两种定义BW.Timer的方法。 Can someone tell me what the difference is? 有人能告诉我有什么区别吗? I am not certain the first is even valid, but if it is valid, what is different about using the myfunc=(function(){}()) syntax? 我不确定第一个是否有效,但如果它有效,那么使用myfunc=(function(){}())语法会有什么不同?

BW.Timer = (function () {
    return {
        Add: function (o) {
            alert(o);
        },
        Remove: function (o) {
            alert(o);
        }
    };
} ());

And... 和...

BW.Timer = function () {
    return {
        Add: function (o) {
            alert(o);
        },
        Remove: function (o) {
            alert(o);
        }
    };
};

The first is the return-value of the immediately-invoked function. 第一个是立即调用函数的返回值。 The second is a function. 第二个是功能。 It essentially comes down to what the difference is between these: 它基本上归结为它们之间的区别:

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

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

Since the first function is called immediately, the value of 0 is given to the variable f . 由于第一个函数是立即调用的,因此将0的值赋给变量f The first f is not a function. 第一个f不是函数。 However, the second f we must call in order to get the value: 但是,我们必须调用第二个f才能获得值:

f(); // 0

It follows that in your example, the first BW.Timer is the object literal itself and the second is a function returning an object literal. 因此,在您的示例中,第一个BW.Timer是对象文本本身,第二个是返回对象文字的函数。 You must call the function in order to get to the object: 您必须调用该函数才能访问该对象:

BW.Timer().Add(x);

Why use the first then? 为什么要先使用呢?

You might ask yourself why one would use a syntax like a = (function() { return {}; })() instead of a = {} , but there's a good reason. 您可能会问自己为什么会使用类似a = (function() { return {}; })()而不是a = {}的语法,但这是有充分理由的。 An IIFE (Immeditately-Invoked Function Expression), unlike a regular function allows the emulation of static variables (variables that maintain their value through a single instance). 与常规函数不同,IIFE(Immeditately-Invoked Function Expression)允许模拟静态变量(通过单个实例维护其值的变量)。 For example: 例如:

var module = (function() {
    var x = 0;

    return {   get: function()  { return x },
               set: function(n) { x = n }
    };

})();

The above a text-book example of the Module Pattern . 以上是模块模式的教科书示例。 Since the function is called right away, the variable x is instantiated and the return value (the object) is given to module . 由于函数是立即调用的,因此变量x被实例化,返回值(对象)被赋予module There's no way we can get to x other than by using the get and set methods provided for us. 除了使用为我们提供的getset方法之外,我们无法访问x Therefore, x is static, meaning its variable won't be overridden each time you use module . 因此, x是静态的,这意味着每次使用module时都不会覆盖其变量。

module.set(5);

module.get(); // 5

On the other hand, let's see an example where module is declared as a function instead: 另一方面,让我们看一个将module声明为函数的示例:

// assume module was made as a function

module().set(5);

module().get(); // 0

When we call module() the x variable is overridden each time. 当我们调用module() ,每次都会覆盖x变量。 So we're effectively using different instances of module and x each time we call module . 所以我们每次调用module时都会有效地使用modulex不同实例。

The difference is rather large. 差异相当大。

In the first case, BW.Timer is executed when it is first encountered, and that is the static version assigned to BW.Timer . 在第一种情况下, BW.Timer在第一次遇到时执行,这是分配给BW.Timer的静态版本。 In that instance, BW.Timer.Add(1) may be used. 在那种情况下,可以使用BW.Timer.Add(1) Each call to BW.Timer will be the same object. 每次调用BW.Timer都是同一个对象。

In the second case, BW.Timer is not executed when first encountered, and instead is a function referece which must be invoked BW.Timer() . 在第二种情况下, BW.Timer在第一次遇到时不会执行,而是一个函数引用,必须调用BW.Timer() For Add to be used, this must be the case BW.Timer().Add(1) . 对于要使用的Add ,必须是BW.Timer().Add(1) Also, you can issue var timer = new BM.Timer(); 此外,您可以发出var timer = new BM.Timer(); . Each instance of BW.Timer() will be unique here. BW.Timer()每个实例在这里都是唯一的。

在第一个示例中, BW.Timer引用自执行函数返回的对象,而在第二个示例中,它引用一个函数对象,换句话说,它是一个可以执行的函数BW.Timer()

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

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