揭示模块模式的缺点 [英] Revealing module pattern disadvantages

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

问题描述

我最近熟悉了显示模块模式,我已经阅读了不少文章。



看起来像是一个非常好的模式,我想开始使用它在一个大项目我有。在我使用的项目中:Jquery,KO,requirejs,Jquery Mobile,JayData。在我看来,这很适合KO ViewModels。



具体来说,我想使用



有一件事我找不到使用这种模式的缺点,是因为没有(我觉得很难相信)?


解决方案

我阅读了@nemesv引用我的文章(谢谢:)),我认为还有一个缺点,没有提及,所以我以为我会添加它在这里供参考。以下是文章的引用:


缺点



模式是,如果私有函数引用
a public函数,那么如果需要补丁
,则该函数不能被覆盖。这是因为私人函数将继续
指私人执行,而且模式不适用于
的公共成员,只适用于函数。



引用私有变量的公共对象成员也是
,但需遵守上面的无补丁规则注释。



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


我的补充:



您不能使用此模式继承。例如:

  var Obj = function(){
//做一些构造函数
}

var InheritingObj = function(){
//做一些构造函数
}

InheritedObj.prototype = new Obj();

InheritedObj.prototype.constructor = InheritingObj;

这是js中继承的一个简单示例,但是当使用显示原型模式,您需要执行此操作:

  InheritingObj.prototype =(function(){
// some prototype stuff here
} ));

这将覆盖您的继承。


I recently got familiar with the Revealing Module pattern and I've read quite a few articles about it.

It seems like a very good pattern and I would like to start using it in a big project I have. In the project I'm using : Jquery, KO ,requirejs, Jquery Mobile, JayData. It seems to me like it'll be a good fit for the KO ViewModels.

In specific I'd like to use THIS version of it.

One thing I could not find are disadvantages for using this pattern, is it because there aren't any (I find it hard to believe)?

What should i consider before starting to use it?

解决方案

I read the article that @nemesv referenced me to (Thanks :)) and I thinks there is one more disadvantage that was not mentioned, so I thought I'd add it here for reference. Here is a quote from the article:

Disadvantages

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.

And my addition:

You can't use inheritance with this pattern. For example:

var Obj = function(){
    //do some constructor stuff
}

var InheritingObj = function(){
    //do some constructor stuff
}

InheritingObj.prototype = new Obj();

InheritingObj.prototype.constructor = InheritingObj;

This a simple example for inheritance in js, but when using the Revealing Prototype Pattern you'll need to do this:

InheritingObj.prototype = (function(){
    //some prototype stuff here
}());

which will override you inheritance.

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

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