In your example, there is no inheritance relationship between homeController and baseController . In addition, this line:
homeController.prototype = baseController;
This is redundant because the prototype property is relevant only to constructor functions.
If you want to configure baseController so that the one who extends / inherits from it has a request listener assigned to it, you must first define it as follows:
var BaseController = function(){ this.on('request', function(){ console.log('...'); }); }; BaseController.prototype = new events.EventEmitter(); BaseController.prototype.constructor = BaseController;
It will be even better if you can assign a handler in the initiliazation method (working in the constructor function is usually bad):
var BaseController = function(){}; BaseController.prototype = new events.EventEmitter(); BaseController.prototype.init = function(){ this.on('request', function(){ console.log('...'); }); };
And then all you need for an extension in the HomeController is to do this:
var HomeController = function(){}; HomeController.prototype = new BaseController(); var homeController = new HomeController(); homeController.init();
Please note that if you do not, creating long inheritance chains in JS is not very pleasant or supported. If you can, I recommend that you avoid this and try to find a more convenient solution.
source share