且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

为什么是模块模式?

更新时间:2021-07-28 21:51:10

我认为这个例子可以帮助你阐明模块模式的用处.

I think this example could help you to clarify the usefulness of the Module Pattern.

模块模式被广泛使用,因为它提供了结构并有助于组织你的代码随着它的增长.与其他语言不同,JavaScript 没有特殊的语法对于包,但模块模式提供了创建自包含解耦的工具可以将其视为功能的黑匣子并添加的代码片段,根据软件的(不断变化的)要求替换或删除你在写.

The module pattern is widely used because it provides structure and helps organize your code as it grows. Unlike other languages, JavaScript doesn’t have special syntax for packages, but the module pattern provides the tools to create self-contained decoupled pieces of code, which can be treated as black boxes of functionality and added, replaced, or removed according to the (ever-changing) requirements of the software you’re writing.

模块模式是几种模式的组合,即:

The module pattern is a combination of several patterns, namely:

  • 命名空间
  • 直接函数
  • 私人和特权会员
  • 声明依赖

第一步是设置命名空间.让我们使用之前的 namespace() 函数在本章中并启动一个示例实用程序模块,该模块提供有用的数组方法:

The first step is setting up a namespace. Let’s use the namespace() function from earlier in this chapter and start an example utility module that provides useful array methods:

MYAPP.namespace('MYAPP.utilities.array');

下一步是定义模块.该模式使用立即函数如果需要隐私,请提供私有范围.立即函数返回一个对象 - 具有公共接口的实际模块,它将可供模块:

The next step is defining the module. The pattern uses an immediate function that will provide private scope if privacy is needed. The immediate function returns an object - the actual module with its public interface, which will be available to the consumers of the module:

 MYAPP.utilities.array = (function () {
    return {
    // todo...
    };
 }());

接下来,让我们在公共接口中添加一些方法:

Next, let’s add some methods to the public interface:

MYAPP.utilities.array = (function () {
   return {
      inArray: function (needle, haystack) {
         // ...
      },
      isArray: function (a) {
         // ...
      }
   };
}());

利用立即函数提供的私有作用域,可以声明一些根据需要使用私有属性和方法.就在立即函数的顶部也将是声明您的模块可能具有的任何依赖项的地方.下列的变量声明,您可以选择放置任何一次性初始化代码帮助设置模块.最终结果是立即函数返回的对象包含模块的公共 API:

Using the private scope provided by the immediate function, you can declare some private properties and methods as needed. Right at the top of the immediate function will also be the place to declare any dependencies your module might have. Following the variable declarations, you can optionally place any one-off initialization code that helps set up the module. The final result is an object returned by the immediate function that contains the public API of your module:

MYAPP.namespace('MYAPP.utilities.array');
MYAPP.utilities.array = (function () {
   // dependencies
   var uobj = MYAPP.utilities.object,
       ulang = MYAPP.utilities.lang,
       // private properties
       array_string = "[object Array]",
       ops = Object.prototype.toString;
       // private methods
       // ...
       // end var
   // optionally one-time init procedures
   // ...
   // public API
   return {
      inArray: function (needle, haystack) {
         for (var i = 0, max = haystack.length; i < max; i += 1) {
            if (haystack[i] === needle) {
               return true;
            }
         }
      },
      isArray: function (a) {
         return ops.call(a) === array_string;
      }
      // ... more methods and properties
   };
}());

模块模式是一种广泛使用且强烈推荐的组织方式代码,尤其是随着它的增长.

The module pattern is a widely used and highly recommended way to organize your code, especially as it grows.

JavaScript 模式,作者 Stoyan Stefanov(奥莱利).版权所有 2010 Yahoo!, Inc.,9780596806750