0

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.

1 Answers1

1

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();
Matt
  • 74,352
  • 26
  • 153
  • 180
  • this will throw an error: Cannot set property 'element' of undefined in the Event (this.element). –  Sep 02 '15 at 14:38
  • @AmandaFerrari: It shouldn't do. It [works for me](http://jsfiddle.net/p9fjv27o/)? You need to add the new line *before* `var event = new Event()`, if you're not doing that already. – Matt Sep 02 '15 at 14:40
  • Hm, i found the error. In the Event i'm calling one method of Event, i can't do that? –  Sep 02 '15 at 14:43
  • @AmandaFerrari: Any other `Event.prototype` additions must be *after* the line I added. See my edit. – Matt Sep 02 '15 at 14:46
  • Hm.. ops, it didn't. I'm going to pass you the real code. –  Sep 02 '15 at 14:50
  • @AmandaFerrari: You cannot set `Event.prototype` to both `EventDirectiveController.prototype` and `EventDirectiveDirective.prototype`... you can only set it to one (as you can only have one constructor). This is seeming more like an [X-Y problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem)... what exactly are you trying to do? – Matt Sep 02 '15 at 14:58
  • Well. I want be able to access the properties of Event in the EventDirectiveDirective and EventDirectiveController, which currently i can, but i need make this two other constructor functions have their own methods. Impossible? –  Sep 02 '15 at 15:06
  • @AmandaFerrari: Again, it seems I'm missing a lot of context around the problem, which makes it hard to provide a solution. This seems to be more of a Angular architectural issue, as the answer I've provided is the way to access the properties of the constructor function. I would advise you to ask a new question, but specify what the problem is you're trying to solve, rather than trying to diagnose the problem you're having with your solution to the problem, [per the guidance here](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). – Matt Sep 02 '15 at 15:21
  • This question is entirely related to pure javascript. Precisely inheritance between prototypes . My question is very simple Math, and even though I was using angular, I could be using without. I want to be able to access the properties of a Super Class in other 2 Super Classes without losing their scope.. –  Sep 02 '15 at 15:31
  • 1
    @AmandaFerrari: They cannot all be super classes. At least one will need to be a subclass. If you want 1 subclass and multiple super classes, what I have posted will work. If you want 2 subclasses and a single super class, then you'll need a solution similar to what has been posted on http://stackoverflow.com/questions/9163341/multiple-inheritance-prototypes-in-javascript. – Matt Sep 02 '15 at 15:43