Search code examples
javascriptmodule-pattern

JS module pattern override function


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


Solution

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