0

Is there a better way of having a class inherit prototype methods from another class and still be able to define new prototype methods on the class that inherits than this:

var ParentConstructor = function(){
};

ParentConstructor.prototype = {
    test: function () {
        console.log("Child");
    }
};

var ChildConstructor = function(){
    ParentConstructor.call(this)
};

ChildConstructor.prototype = {

    test2: "child proto"
};

var TempConstructor = function(){};
TempConstructor.prototype = ParentConstructor.prototype;
ChildConstructor.prototype = new TempConstructor();
ChildConstructor.prototype.constructor = ChildConstructor;



var child = new ChildConstructor();

child.test();
console.log(child.test2)

console.log(child, new ParentConstructor());

Which isn't working, because I lose my test2 prototype method / property when I add the inheritance from my ParentConstructor.

I've tried other ways to extend the prototype methods of a class with some prototype props form other classes but I have failed each time, because I couldn't find a way not to override the previous methods each time.

I have also tried the var Child = Object.create(Parent.Prototype), but when I define new props I lose the parent props.

Roland
  • 9,321
  • 17
  • 79
  • 135

2 Answers2

2

Setting up inheritance should take place before you define new properties on the prototype of ChildConstructor. And when you define new prototype properties, you also shouldn't override the whole prototype property. Instead, you can simply add new properties, like you already did with the constructor property:

ChildConstructor.prototype = new ParentConstructor();
ChildConstructor.prototype.constructor = ChildConstructor;

ChildConstructor.prototype.test2 = "child proto";
basilikum
  • 10,378
  • 5
  • 45
  • 58
  • Is it necessary to set the constructor each time I define prototype properties ? I'm just curios because when I use a simple class without inheriting I see no difference with or without the constructor set. – Roland Aug 14 '13 at 07:59
  • @rolandjitsu the constructor property is in most cases not really neccesary at all, except you want to use it for yourself or you are using a library that somehow uses this property. But anyway, you don't need to set it every time you define prototype properties, you only need to set it when you override the entire `prototype` property, like you do when setting up inheritance. But `ChildConstructor.prototype.test2 = "child proto";` doesn't have any effect on the `constructor` property. – basilikum Aug 14 '13 at 08:43
0

The best example I can think of comes from:

http://robertnyman.com/2008/10/06/javascript-inheritance-how-and-why/

function Being() {
    this.living = true;
    this.breathes = function () {
       return true;
    };
}

function Robert() {
    // Robert.prototype = new Being(); /* edit */
    this.blogs = true;
    this.getsBored = function () {
        return "You betcha";
    };
}

Robert.prototype = new Being(); 

Robert.prototype.newMethod = function() {
    console.log('new method executed');
    return this;
}

Note this example, has been updated, the first comment below is directed at the first code I had up, which contained the prototype inside the Robert method.

Michael Benin
  • 4,317
  • 2
  • 23
  • 15
  • This is weird because multiple Robert instances will not share the same prototype object. Check it out: http://jsfiddle.net/aDCmA/ – bfavaretto Aug 13 '13 at 20:01
  • That is strange, if you move prototype below it behaves as expected: http://jsfiddle.net/aDCmA/1/ – Michael Benin Aug 14 '13 at 16:26
  • Yes, when you move it outside the constructor, you only instance Being once; inside the constructor, each instance of Robert was getting a new, separate instance of Being as its prototype. – bfavaretto Aug 14 '13 at 17:19