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());
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/
I-Lin Kuo Dec 31 '14 at 11:33 2013-12-31 11:33
source share