开发者

How to make a submodule via 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 c开发者_如何学Pythonhild class of it with properties inherited from parent? How can I do the same with module pattern?


The 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.com/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.com/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 Pri­vate 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 becomes 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜