Javascript 用冒号​​返回

我正在学习 JavaScript,并且遇到了以下结构:

I am learning JavaScript and have come across of the structure below:

var Test = (function () {

  function func1() {
      //do something.....
  }

  function func2() {
      //do something.....
  }

  function func3() {
      //do something.....
  }

  return {
      func1: func1,
      func2: func2,
      func3: func3
  };

})();

我想知道返回块在做什么.这是一个非常常用的 JavaScript 结构吗?请让我知道在哪里可以获得有关此的更多信息.

I am wondering what the return block is doing. Is this a very commonly used JavaScript structure? Please let me know where can I get more information about this.

推荐答案

这是Revealing Module Pattern.

返回的对象包含对 IIFE 中定义的函数的引用.所以里面定义的函数对于匿名函数是私有的.

The returned object contains references to the functions defined inside the IIFE. So the functions defined inside are private to the anonymous function.

但是如果你想在外面使用内部函数,你可以使用返回的对象.

But if you want to use the inner functions outside, you can use the returned object.

Test 的值为

var Test = {
    func1: func1,
    func2: func2,
    func3: func3
};

你可以从外部调用func1

Test.func1();

这是 Javascript emulate class 的方式.由于没有使用模块模式的可见性说明符,因此可以将变量/方法设为公共/私有.

This is the way Javascript emulate class. As there is no visibility specifiers using Module pattern, variables/methods can be make public/private.

显示模块模式的灵感来自于模块模式.在揭示模块模式中,object 中只返回对 private 变量/方法的引用.

The revealing module pattern is inspired from Module pattern. In revealing module pattern, only reference to the private variables/methods is returned in an object.

该模式背后的主要思想是避免邪恶的全局变量.这看起来类似于 IIFE,除了返回一个对象而不是函数.IIFE 中定义的变量/方法对于函数是私有的.要访问 IIFE 内部的任何变量/方法,需要将其添加到返回的对象中,然后才能从 IIFE 外部访问它.这种模式利用了闭包,因此在 IIFE 中定义的变量/方法即使在对象返回后也可以访问.

The main idea behind the pattern is avoiding evil global variables. This looks similar to IIFE except an object is returned instead of function. The variables/methods defined inside the IIFE are private to the function. To access any variable/method inside the IIFE, it needs to be added in the returned object and then it can be accessed from outside of IIFE. This pattern takes advantage of closures, so the variables/methods defined inside the IIFE are accessible even after the object is returned.

来自 Addy Osmani 的书学习 Javascript 设计模式

From Addy Osmani's book Learning Javascript Design patterns

Revealing Module 模式的出现是因为 Heilmann 对当我们想要从另一个公共方法调用或访问公共变量时必须重复主对象的名称这一事实感到沮丧.他也不喜欢模块模式的要求,即他希望公开的东西必须切换到对象字面量表示法.

The Revealing Module pattern came about as Heilmann was frustrated with the fact that he had to repeat the name of the main object when we wanted to call one public method from another or access public variables. He also disliked the Module pattern’s requirement for having to switch to object literal notation for the things he wished to make public.

他努力的结果是更新了模式,我们只需在私有范围内定义所有函数和变量,并返回一个匿名对象,其中包含指向我们希望公开为私有功能的指针.

The result of his efforts was an updated pattern where we would simply define all of our functions and variables in the private scope and return an anonymous object with pointers to the private functionality we wished to reveal as public.

优点:

  1. 封装.IIFE 内部的代码是从外部世界封装的
  2. 干净、有条理和可重用的代码
  3. 隐私.它允许创建私有变量/方法.不能从 IIFE 外部访问私有变量/方法.

缺点:

  1. 如果私有函数引用公共函数,则该公共函数不能被覆盖

进一步阅读:

  1. https://en.wikipedia.org/wiki/Module_pattern
  2. https://carldanley.com/js-revealing-module-pattern/
  3. 如何在 JavaScript 中使用 Revealing 模块模式

编辑

来自 评论来自 @Mike

值得注意的是,通常创建一个对象(例如,var me = {};)然后在其上声明可能的公共成员(me.func1 = function() {/* ... */};),最后返回那个对象(return me;).这避免了我们在 OP 代码的 return 语句中看到的重复(所有公共内容都重复).

It's of note that it's common to create an object (eg, var me = {};) and then declare the would-be public members on it (me.func1 = function() { /* ... */ };), returning that object at the end (return me;). This avoids the repetition that we see in the return statement of OP's code (where all the public stuff is repeated).

相关文章