Inheritance of closure objects and overriding of methods

Posted by bobikk on Stack Overflow See other posts from Stack Overflow or by bobikk
Published on 2010-03-18T14:59:27Z Indexed on 2010/03/18 15:11 UTC
Read the original article Hit count: 335

I need to extend a class, which is encapsulated in a closure. This base class is following:

var PageController = (function(){

    // private static variable
    var _current_view;

return function(request, new_view) {
    ...

    // priveleged public function, which has access to the _current_view 
    this.execute = function() {
        alert("PageController::execute");
    }

}
})();

Inheritance is realised using the following function:

function extend(subClass, superClass){
var F = function(){
};
F.prototype = superClass.prototype;
subClass.prototype = new F();
subClass.prototype.constructor = subClass;
subClass.superclass = superClass.prototype;
StartController.cache = '';


if (superClass.prototype.constructor == Object.prototype.constructor) {
    superClass.prototype.constructor = superClass;
}
}

I subclass the PageController:

var StartController = function(request){
    // calling the constructor of the super class
    StartController.superclass.constructor.call(this, request, 'start-view');
}
// extending the objects
extend(StartController, PageController);

// overriding the PageController::execute
StartController.prototype.execute = function() {
alert('StartController::execute');
}

Inheritance is working. I can call every PageController's method from StartController's instance. However, method overriding doesn't work:

var startCont = new StartController();
startCont.execute();

alerts "PageController::execute". How should I override this method?

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about inheritance