문제

I have following 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();

Can you tell me how can I adjust my module pattern inspired by Douglas CrockFord to fully override alerNumber function? So far showNumber function displays 10 instead of 20.

Thank you all in advanced

JSFiddle with code is here

도움이 되었습니까?

해결책

You could change

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

to

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

But I'm not sure I see why you don't simply use the prototype-base inheritance model.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top