问题描述
我最近熟悉了 Revealing Module 模式,并且阅读了很多关于它的文章.
这似乎是一个非常好的模式,我想在我拥有的一个大项目中开始使用它.在我正在使用的项目中:Jquery、KO、requirejs、Jquery Mobile、JayData.在我看来,它非常适合 KO ViewModel.
具体来说,我想使用 这个版本.
我找不到使用这种模式的缺点,是因为没有任何缺点(我很难相信)?
在开始使用之前我应该考虑什么?
推荐答案
我阅读了@nemesv 引用我的文章(谢谢:)),我认为还有一个没有提到的缺点,所以我想我'd 在此处添加以供参考.以下是文章的引述:
<块引用>缺点
这种模式的一个缺点是,如果私有函数引用一个公共函数,如果一个补丁,这个公共函数不能被覆盖有必要的.这是因为私有函数将继续参考私有实现,该模式不适用于公共成员,仅限函数.
因此,使用 Revealing Module 模式创建的模块可能比使用原始模块创建的更脆弱模式,所以在使用过程中要小心.
还有我的补充:
您不能在此模式中使用继承.例如:
var Obj = function(){ //do some constructor stuff } var InheritingObj = function(){ //do some constructor stuff } InheritingObj.prototype = new Obj(); InheritingObj.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.