为什么模块模式? [英] why module pattern?

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

问题描述

我已经阅读了很多有关模块模式的内容。
Ok它带来结构,私有方法等...
但是使用下面的代码,我可以得到相同的行为,而不使用它。

I've read a lot of things about the module pattern. Ok It brings structure, private method, etc... But with the code below I can get the same behavior without using it.

function Human()
{
  // private properties
  var _name='';
  var _age=0;


  // private methods
  function created()
  {
    console.log("Human "+_name+" called");
  };

  // public
  this.setName = function(name){
    _name=name;
    created(); 
  };

}


var h1 = new Human();

h1.setName("John");

那么最后一个模块模式的真正优势是什么?

So, what are the real advantage of a module pattern finally ?

推荐答案

我认为这个例子可以帮助您澄清模块模式的有用性。

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:


  • 命名空间

  • 立即功能

  • 私人和特权成员

  • 声明依赖关系

  • Namespaces
  • Immediate functions
  • Private and privileged members
  • Declaring dependencies

第一步是设置命名空间。让我们在本章中使用早期
中的 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) {
         // ...
      }
   };
}());

使用立即函数提供的私有范围,您可以声明一些
私有属性,方法根据需要。在直接函数$ ​​b $ b的顶部,也将是您的模块声明任何依赖关系的地方。在
之后的变量声明中,您可以选择放置任何一次性初始化代码,
帮助设置模块。最终结果是一个直接函数$ ​​b $ b返回的对象,它包含您的模块的公共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
(O'Reilly)。版权所有2010 Yahoo !, Inc.,9780596806750

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

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