Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 410
Ondřej Severa Avatar asked May 31 '13 14:05

Ondřej Severa


1 Answers

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.

like image 177
Denys Séguret Avatar answered Sep 30 '22 14:09

Denys Séguret