Identify module model flaws

I recently got acquainted with the Revealing Module template, and I read several articles about it.

This seems to be a very good sample, and I would like to start using it in a large project that I have. In the project I use: JQuery, KO, requirejs, JQuery Mobile, JayData. It seems to me that this will be well suited for ViewModels KO.

In particular, I would like to use this version.

One thing I could not find is the disadvantages of using this template, because no (I find it hard to believe)?

What should I consider before using it?

+11
javascript design-patterns revealing-module-pattern
Dec 27
source share
3 answers

I read an article in which @nemesv refers to me (thanks :)), and I think there is another drawback that was not mentioned, so I thought I'd add it here for reference. Here is a quote from the article:

disadvantages

The disadvantage of this template is that if a private function refers to a public function, this public function cannot be overridden if a patch is needed. This is because the private function will continue to refer to the private implementation, and the template does not apply to public members, only for functions.

Public elements of an object that refer to private variables, also taking into account the above rule notes without a patch.

As a result, modules created using the drop-down module template may be more fragile than those created using the original template module, so care should be taken during use.

And my addition:

You cannot use inheritance with this template . 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 is a simple example for inheritance in js, but when using the Revealing Prototype Pattern you will need to do this:

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

which will override your inheritance.

+4
Dec 31 '13 at 7:14
source share
— -

The expansion module template (RMP) creates objects that do not behave well with respect to overriding. As a result, objects created using RMP do not work as well as prototypes. Therefore, if you use RMP to create objects that will be used in the inheritance chain, just do not. This point of view is my own, as opposed to those who favor a prototype of the disclosing prototype.

To see the inheritance behavior incorrectly, do the following URL builder example:

 function rmpUrlBuilder(){ var _urlBase = "http://my.default.domain/"; var _build = function(relUrl){ return _urlBase + relUrl; }; return { urlBase: _urlBase, build: _build } } 

Casting aside the question of why you are using RMP for an object without any private components, note that if you take the returned object and override urlBase with /qaru.site / ... ", you can expect the build ( ) will change accordingly. It is not as shown below:

 var builder = new rmpUrlBuilder(); builder.urlBase = "http://qaru.site/"; console.log(builder.build("/questions"); // prints "http://my.default.domain/questions" not "http://qaru.site/questions" 

Contrast the behavior with the following implementation of the URL builder.

 function urlBuilder = function(){ return { urlBase: "http://my.default.domain/". build: function(relUrl){ return this.urlBase + relUrl;} } } var builder = new urlBuilder(); builder.urlBase = "http://qaru.site/"; console.log(builder.build()); // prints "http://qaru.site/questions" 

which behaves correctly.

You can fix the behavior of the Revealing Module Pattern using this scope as shown below.

 function rmpUrlBuilder(){ var _urlBase = "http://my.default.domain/"; var _build = function(relUrl){ return this.urlBase + relUrl; }; return { urlBase: _urlBase, build: _build } } 

but it rather hits the target of the popup module template. For more details, see my blog post http://ilinkuo.wordpress.com/2013/12/28/defining-return-object-literals-in-javascript/

+13
Dec 31 '14 at 11:33
source share

Other disadvantages with the drop-down module template include:

  • Close association of public functions with the Return statement
  • Mixed use of object literals for public functions and autonomous declarations for private functions

I would recommend using the template of the final module according to the template of the drop-down module ( https://github.com/tfmontague/definitive-module-pattern )

-2
Jul 07 '14 at 8:40
source share



All Articles