什么是这个JavaScript模式调用,为什么使用它? [英] What is this JavaScript pattern called and why is it used?

查看:155
本文介绍了什么是这个JavaScript模式调用,为什么使用它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在学习 THREE.js ,注意到一个模式,其中的函数定义如下:

  var foo =(function(){
var bar = new Bar();

return function ){
//使用bar从上面的实际逻辑
//返回结果;
};
}());

(示例请参见raycast方法此处)。



em>这样的方法的变体看起来像这样:

  var foo = function(){
var bar = new Bar();

//实际逻辑。
// return result;
};

将第一个版本与正常版本进行比较,


  1. 它分配自执行函数的结果。


此函数中的局部变量。

  • 它返回包含使用局部变量的逻辑的< >

    所以主要的区别是,在第一个变体中,bar只在初始化时分配一次,而第二个变量在每次调用时创建这个临时变量。



    我最好猜猜为什么使用它是为了限制bar的实例数(只有一个),从而节省内存管理开销。



    我的问题:


    1. 这个假设是否正确?


    2. 解决方案 / div>

      您的假设几乎是正确的。



      1. 分配自动执行功能的返回


      这称为立即调用的函数表达式 IIFE



      1. 在此函数中定义一个局部变量


      这是在JavaScript中拥有私有对象字段的方式,因为它不提供 private



      1. 包含使用局部变量的逻辑的实际函数。



      这个模式是否有名称?


      AFAIK您可以将此模式称为模块模式。引用:


      模式模式使用闭包封装隐私,状态和组织。它提供了一种方法来包装公共和私有方法和变量的混合,保护片段不泄漏到全局范围,并意外地与另一个开发人员的界面碰撞。使用此模式,只返回一个公开的API,保持闭包中的其他内容都是私有的。


      比较这两个例子,我最好的猜测关于为什么使用第一个是:


      1. 正在实现Singleton设计模式。

      2. 可以使用第一示例控制可以创建特定类型的对象的方式。与此点的一个接近匹配可以是静态工厂方法,如有效Java。

      3. 如果每次都需要相同的对象状态,它就是效率

      但是如果你每次只需要vanilla对象,那么这个模式可能不会添加任何值。


      I'm studying THREE.js and noticed a pattern where functions are defined like so:

      var foo = ( function () {
          var bar = new Bar();
      
          return function ( ) {
              //actual logic using bar from above.
              //return result;
          };
      }());
      

      (Example see raycast method here).

      The normal variation of such a method would look like this:

      var foo = function () {
          var bar = new Bar();
      
          //actual logic.
          //return result;
      };
      

      Comparing the first version to the normal variation, the first seems to differ in that:

      1. It assigns the result of a self-executing function.
      2. It defines a local variable within this function.
      3. It returns the actual function containing logic that makes use of the local variable.

      So the main difference is that in the first variation the bar is only assigned once, at initialization, while the second variation creates this temporary variable every time it is called.

      My best guess on why this is used is that it limits the number of instances for bar (there will only be one) and thus saves memory management overhead.

      My questions:

      1. Is this assumption correct?
      2. Is there a name for this pattern?
      3. Why is this used?

      解决方案

      Your assumptions are almost correct. Let's review those first.

      1. It assigns the return of a self-executing function

      This is called an Immediately-invoked function expression or IIFE

      1. It defines a local variable within this function

      This is the way of having private object fields in JavaScript as it does not provide the private keyword or functionality otherwise.

      1. It returns the actual function containing logic that makes use of the local variable.

      Again, the main point is that this local variable is private.

      Is there a name for this pattern?

      AFAIK you can call this pattern Module Pattern. Quoting:

      The Module pattern encapsulates "privacy", state and organization using closures. It provides a way of wrapping a mix of public and private methods and variables, protecting pieces from leaking into the global scope and accidentally colliding with another developer's interface. With this pattern, only a public API is returned, keeping everything else within the closure private.

      Comparing those two examples, my best guesses about why the first one is used are:

      1. It is implementing the Singleton design pattern.
      2. One can control the way an object of a specific type can be created using the first example. One close match with this point can be static factory methods as described in Effective Java.
      3. It's efficient if you need the same object state every time.

      But if you just need the vanilla object every time, then this pattern will probably not add any value.

      这篇关于什么是这个JavaScript模式调用,为什么使用它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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