最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to make a submodule via module pattern - Stack Overflow

programmeradmin0浏览0评论

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
Add a ment  | 

2 Answers 2

Reset to default 7

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./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 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 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.

发布评论

评论列表(0)

  1. 暂无评论