如果需要补丁,则不能覆盖“公共功能”的含义。在Addy的Revealing Module Pattern描述中? [英] What is meant by "public function can't be overridden if a patch is necessary." in Addy's description of the Revealing Module Pattern?

查看:121
本文介绍了如果需要补丁,则不能覆盖“公共功能”的含义。在Addy的Revealing Module Pattern描述中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


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

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.



<有没有人有这样的例子?

Does anyone have an example of what he means by this?

链接到上面引用的揭示模块模式

推荐答案

比较使用创建的对象一个对象文字,由一个由Revealing Module Pattern创建的。

Compare an object created by using an object literal to one created by the Revealing Module Pattern.

这是一个作为对象文字创建的。

Here is one created as an object literal.

function makeGreeter(name){
  return {
    getName: function(){ return name;},
    sayHello: function(){console.log("Hello, " + this.getName());}
  }
}

var greeter = makeGreeter("Danny");
greeter.sayHello; // "Hello, Danny"
greeter.getName = function(){ return "George";}
greeter.sayHello(); // "Hello, George"

覆盖公共方法 getName 在返回的对象上, sayHello 方法取决于 getName 获取更改。这是因为在Object Literal样式中,对公共函数的引用是通过 this ,返回的对象。

When you override the public method getName on the returned object, the sayHello method which depends on getName picks up the change. This is because in the Object Literal style, references to public functions are made via this, the returned object.

但是,当您使用Revealing Module Pattern时,

However, when you use the Revealing Module Pattern,

function makeGreeter(name){
  var getName = function(){ return name;},
    sayHello = function(){console.log("Hello, " + getName());};
  return {
    getName: getName,
    sayHello: sayHello
  }
}

var greeter = makeGreeter("Danny");
greeter.sayHello; // "Hello, Danny"
greeter.getName = function(){ return "George";}
greeter.sayHello(); // "Hello, Danny"

RMP欢迎不会接受公开的覆盖 getName 方法。这是因为当RMP函数引用其他函数(公共和私有)时,它们引用私有闭包副本而不是附加到返回对象的公共函数。

The RMP greeter will not pick up the override to the public getName method. This is because when RMP functions reference other functions (both public and private), they refer to the private closure copy rather than to the public function attached to the returned object.

因此我将Revealing Module Pattern视为反模式。

It is for this reason I regard the Revealing Module Pattern as an anti-pattern.

这篇关于如果需要补丁,则不能覆盖“公共功能”的含义。在Addy的Revealing Module Pattern描述中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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