Reputation:
i'm trying to inherit properties from one constructor function in other constructor function.
I have my Event constructor function:
var Controller = require('./controller');
var Event = function() {
this.element = 'element';
Controller.call(this);
};
var event = new Event();
And my Controller constructor function:
var Controller = function() {
console.log(this.element) // prints 'element'.
this.method(); // < error
};
Controller.prototype.method = function() {
console.log(this.element);
};
module.exports = Controller;
I already can access the Event properties and methods in the Controller, but i can't create my own controller methods.
Can by solved?
Thanks.
Upvotes: 0
Views: 46
Reputation: 75317
You should set the prototype
of Event
. This will add Controller
to Event
's prototype chain, resulting in the Controller
's methods being available on the Event
's instance.
var Controller = require('./controller');
var Event = function() {
this.element = 'element';
Controller.call(this);
};
// See here!
Event.prototype = Object.create(Controller.prototype);
// Note other Event.prototype additions must be *after* the above line.
Event.prototype.another = function () {
console.log('Another');
this.method();
};
var event = new Event();
Upvotes: 1