JS模块模式覆盖功能

时间:2013-05-31 14:22:51

标签: javascript module-pattern

我有以下模式

BASE = function () {
    var that = {};
    var number = 10;

    that.showNumber = function(){
        that.alertNumber();
    }

    that.alertNumber = function () {
        alert(number);
    };    
    return that;
};

CHILD = function () {
    var that = Object.create(BASE());
    var secondNumber = 20;

    // Override base function
    that.alertNumber = function () {
        alert(secondNumber);
    };
    return that;
};

var ch = CHILD();
ch.showNumber();

您能告诉我如何调整受Douglas CrockFord启发的模块模式以完全覆盖alerNumber功能?到目前为止,showNumber函数显示10而不是20。

先谢谢大家

JSFiddle代码为here

1 个答案:

答案 0 :(得分:5)

你可以改变

that.showNumber = function(){
    that.alertNumber();
}

that.showNumber = function(){
    this.alertNumber();
}

但我不确定为什么你不会简单地使用the prototype-base inheritance model