admin管理员组文章数量:1335445
I was reading about JavaScript Module pattern. My Question is how do I make submodules with it, i.e how can I inherit from it, say I have this class
var MODULE = (function () {
my = function(){
this.params = ""
},
privateVariable = 1;
my.prototype.moduleMethod = function () {
console.log("mod");
};
return my;
}());
How do I make a child class of it with properties inherited from parent? How can I do the same with module pattern?
I was reading about JavaScript Module pattern. My Question is how do I make submodules with it, i.e how can I inherit from it, say I have this class
var MODULE = (function () {
my = function(){
this.params = ""
},
privateVariable = 1;
my.prototype.moduleMethod = function () {
console.log("mod");
};
return my;
}());
How do I make a child class of it with properties inherited from parent? How can I do the same with module pattern?
Share Improve this question edited Sep 22, 2011 at 2:44 Azder 4,7287 gold badges38 silver badges57 bronze badges asked Sep 22, 2011 at 2:28 Rocky SinghRocky Singh 15.4k31 gold badges106 silver badges146 bronze badges 1- Oh, and as RobG noted, ALWAYS use var for variables, never ommit it, and remember that it's function scoped, not block scoped. – Azder Commented Sep 22, 2011 at 2:51
2 Answers
Reset to default 7The module pattern is not a class pattern. You cannot simply pretend you now have classes in JavaScript. As for inheritance, if you really need to inherit stuff, you should make an object via constructor function and use prototypal inheritance, although it's sometimes slower to execute.
As for creating a submodule it's simple
MODULE.submodule = (function(){
// another module stuff that can even reference MODULE
return { submodule: 'property' }
})();
Now, as for subclassing in the classical sense, you can simulate it on objects with prototypes, like Douglas Crockford does http://www.crockford./javascript/inheritance.html
For simulating it with modules, you can try by creating a seal/unseal functions inside the original module and use them in your submodules. You can check here http://www.pallavlaskar./javascript-module-pattern-in-details/ for the
Cloning and Inheritance
var MODULE_TWO = (function (old) {
var my = {},
key;
for (key in old) {
if (old.hasOwnProperty(key)) {
my[key] = old[key];
}
}
var super_moduleMethod = old.moduleMethod;
my.moduleMethod = function () {
// override method on the clone, access to super through super_moduleMethod
};
return my;
}(MODULE))
or the
Cross-File Private State
var MODULE = (function (my) {
var _private = my._private = my._private || {},
_seal = my._seal = my._seal || function () {
delete my._private;
delete my._seal;
delete my._unseal;
},
_unseal = my._unseal = my._unseal || function () {
my._private = _private;
my._seal = _seal;
my._unseal = _unseal;
};
// permanent access to _private, _seal, and _unseal
return my;
}(MODULE || {}));
> var MODULE = (function () {
> my = function(){
If my is not declared with var, it bees global when the function executes. Also, by convention constructors have names starting with a capital letter, so:
var My = function(){
but you may as well just declare the function and be done with it:
function My() {
.
> this.params = ""
> },
> privateVariable = 1;
>
> my.prototype.moduleMethod = function () {
> console.log("mod");
> };
If you are just implementing prototype inheritance, why use the module pattern at all?
>
> return my; }());
The module pattern is not meant for inheritance but to create "modules" of functionality and emulate public, priveleged and private members to some extent.
本文标签: javascriptHow to make a submodule via module patternStack Overflow
版权声明:本文标题:javascript - How to make a submodule via module pattern - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742374560a2462901.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论