如何通过模块模式创建子模块

发布于 2024-12-05 14:30:55 字数 364 浏览 1 评论 0原文

我正在阅读有关 JavaScript 模块模式的内容。我的问题是如何用它创建子模块,即如何从它继承,假设我有这个类,

    var MODULE = (function () { 
    my = function(){
            this.params = ""
         }, 
    privateVariable = 1; 

    my.prototype.moduleMethod = function () {
        console.log("mod");
    }; 

    return my; 
}());

如何使用从父级继承的属性创建它的子类?我怎样才能对模块模式做同样的事情?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

伪装你 2024-12-12 14:30:55

模块模式不是类模式。你不能简单地假装你现在有 JavaScript 类。至于继承,如果你确实需要继承东西,你应该通过构造函数创建一个对象并使用原型继承,尽管有时执行速度较慢。

至于创建子模块很简单

MODULE.submodule = (function(){
    // another module stuff that can even reference MODULE
    return { submodule: 'property' }
})();

现在,对于经典意义上的子类化,您可以在具有原型的对象上进行模拟,就像 Douglas Crockford 所做的那样 http://www.crockford.com/javascript/inheritance.html

要使用模块进行模拟,您可以尝试在原始模块内创建 seal/unseal 函数并在子模块中使用它们。您可以在此处查看 http://www.pallavlaskar.com/javascript-module-pattern-in-details /
用于

克隆和继承

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))

跨文件私有状态

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 || {}));

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 || {}));
花伊自在美 2024-12-12 14:30:55
>     var MODULE = (function () { 
>     my = function(){

如果my没有用var声明,那么当函数执行时它就会变成全局的。另外,按照惯例,构造函数的名称以大写字母开头,因此:

      var My = function(){

但您也可以只声明该函数并完成它:

      function My() {

>             this.params = ""
>          }, 
>     privateVariable = 1; 
> 
>     my.prototype.moduleMethod = function () {
>         console.log("mod");
>     }; 

如果您只是实现原型继承,为什么还要使用模块模式呢?

> 
>     return my;  }());

模块模式并不是为了继承,而是为了创建功能“模块”并在某种程度上模拟公共、特权和私有成员。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文