JavaScript设计模式:模块模式和显示模块模式之间的区别? [英] JavaScript design pattern: difference between module pattern and revealing module pattern?

查看:147
本文介绍了JavaScript设计模式:模块模式和显示模块模式之间的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在读书最近学习JavaScript设计模式。我不知道的是模块模式和显示模块模式之间的区别。我觉得他们是一回事。任何人都可以举个例子?

I'm reading the book Learning JavaScript Design Patterns recently. What I don't get is the difference between module pattern and revealing module pattern. I feel they are the same thing. Anyone can give an example?

推荐答案

至少有三种实现模块模式的方式,但是显示模块模式是

There are at least three different ways to implement the Module Pattern, but the Revealing Module Pattern is the only Module Pattern descendant that has an official name.

模块模式必须满足以下:

The Module Pattern must satisfy the following:


  • 私人会员住在关闭中。

  • 公开会员在退货对象。

但是这个定义有很多歧义。通过不同的解决模糊,您可以获得模块模式的变体。

But there's a lot of ambiguity in this definition. By resolving the ambiguity differently, you get variants of the Module Pattern.

显示模块模式是模块模式中最着名和最受欢迎的模式。它比其他替代方案具有许多优点,例如

The Revealing Module Pattern is the most famous and most popular of the Module Pattern variants. It has a number of advantages over the other alternatives, such as


  • 重命名公共功能而不改变功能体。

  • 通过修改单行,而不更改功能体,将成员从公共组织更改为私人,反之亦然。

RMP除了原始内容之外,还要满足三个附加条件:

The RMP satisfies three additional conditions in addition to those in the original:


  • 所有成员(无论是公共的还是私有的)都在闭包中定义。 li>
  • 返回对象是一个没有函数定义的对象文字。所有右侧的表达式都是闭包变量

  • 所有引用都是通过闭包变量而不是返回对象。

以下示例显示如何使用

var welcomeModule = (function(){
  var name = "John";
  var hello = function(){ console.log("Hello, " + name + "!");}
  var welcome = function() { console.log( hello() + " Welcome to StackOverflow!");}
  return {
    name: name,
    sayHello: hello,
    sayWelcome: welcome
  }
})();

如果您想使名称 sayHello private,你只需要注释掉返回对象中的相应行。

If you wanted to make name and sayHello private, you just need to comment out the appropriate lines in the return object.

var welcomeModule = (function(){
  var name = "John";
  var hello = function(){ console.log("Hello, " + name + "!");}
  var welcome = function() { console.log( hello() + " Welcome to StackOverflow!");}
  return {
    //name: name,
    //sayHello: hello,
    sayWelcome: welcome
  }
})();



具有对象文字的模块模式



这可能是模块模式中最旧的变体。与RMP不同,此变体没有性感的官方名称。

The Module Pattern with Object Literal

This is probably the oldest variant of the Module Pattern. Unlike RMP, there's no sexy official name for this variant.

除原始内容外,还满足以下条件:

It satisfies the following conditions, in addition to the original:


  • 私人成员在关闭中定义。

  • 公共成员在返回对象文字中定义。

  • 对公共成员的引用通过

  • Private members are defined in the closure.
  • Public members are defined in the return object literal.
  • References to public members are via this, whenever possible.

在下面的示例中,您可以看到如何,与RMP ,函数定义实际上在返回对象文字中,对成员的引用由 限定。

In the following example, you can see how, in contrast to RMP, the function definitions are actually in the return object literal, and references to members are qualified by this.

var welcomeModule = (function(){
  return {
    name: "John",
    sayHello: function(){ console.log("Hello, " + this.name + "!");}
    sayWelcome: function() { console.log( this.hello() + " Welcome to StackOverflow!");}
  }
})();

请注意,为了使名称 sayHello private,引用指向名称 sayHello 在各种函数体中的定义也必须更改。

Note that unlke RMP, in order to make name and sayHello private, the references pointing to name and sayHello in the various function body definitions also have to be changed.

var welcomeModule = (function(){
  var name: "John";
  var sayHello = function(){ console.log("Hello, " + name + "!");};
  return {
    //name: "John",
    //sayHello: function(){ console.log("Hello, " + this.name + "!");}
    sayWelcome: function() { console.log( hello() + " Welcome to StackOverflow!");}
  }
})();



返回对象存根的模块模式



The Module Pattern with Return Object Stub

This variant also has no official name.

除原始内容外,还满足以下条件:

It satisfies the following conditions, in addition to the original:


  • 开始时定义一个空的返回对象存根

  • 私有成员在闭包中定义。

  • 公开成员被定义为存根的成员

  • 通过存根对象引用公共成员

  • An empty return object stub is defined at the beginning.
  • Private members are defined in the closure.
  • Public members are defined as members of the stub
  • References to public members are via the stub object

使用我们的旧例子,你可以看到公共成员被直接添加到stub对象。

Using our old example, you can see that public members are directly added to the stub object.

var welcomeModule = (function(){
  var stub = {};
  stub.name = "John";
  stub.sayHello = function(){ console.log("Hello, " + stub.name + "!");}
  stub.sayWelcome = function() { console.log( stub.hello() + " Welcome to StackOverflow!");}
  return stub;
})();

如果要使名称 sayHello 如前所述,现在私人会员的引用必须更改。

If you want to make name and sayHello private as before, the references to the now-private members have to be changed.

var welcomeModule = (function(){
  var stub = {};
  var name = "John";
  var sayHello = function(){ console.log("Hello, " + name + "!");}

  stub.sayWelcome = function() { console.log( hello() + " Welcome to StackOverflow!");}
  return stub;
})();



摘要



显示之间的差异模块模式和模块模式的其他变体主要在于如何引用公共成员。因此,RMP易于使用和修改,这是其受欢迎程度。然而,这些优势在我看来是非常成本的,Addy Osmani在他的帖子中提到了显示模块模式


这种模式的一个缺点是,如果私有函数是指一个公共函数,如果需要补丁,则不能覆盖该公共函数。这是因为私有函数将继续引用私有实现,并且模式不适用于公共成员,仅适用于函数。

A disadvantage of this pattern is that if a private function refers to a public function, that public function can't be overridden if a patch is necessary. This is because the private function will continue to refer to the private implementation and the pattern doesn't apply to public members, only to functions.

引用的公共对象成员私有变量也受上述无补丁规则注释的限制。

Public object members which refer to private variables are also subject to the no-patch rule notes above.

因此,使用显示模块模式创建的模块可能比使用原始的模块模式,因此在使用过程中应该小心。

As a result of this, modules created with the Revealing Module pattern may be more fragile than those created with the original Module pattern, so care should be taken during usage.

我在某些其他 帖子

这篇关于JavaScript设计模式:模块模式和显示模块模式之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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