1

I am trying to understand inheritance in javascript. I wrote a basic example to implement a simple inheritance between the parent object and a child. But I think there is something wrong when resetting the constructor.

function Mammal(){
}
function Dog(){

}
Mammal.prototype.walk = function(){
    console.log('walking..walking....');
}
Dog.prototype.bark = function(){
    console.log('wof wof!');
}

Dog.prototype = Object.create(Mammal.prototype);
Dog.prototype.constructor = Dog;
let dogInstance = new Dog();
dogInstance.walk();
dogInstance.bark();
eren
  • 13
  • 2
  • "*I think there is something wrong when resetting the constructor.*" - *what* do you think is wrong with that line? – Bergi Oct 14 '18 at 14:27

3 Answers3

0

Problem here Dog.prototype = Object.create(Mammal.prototype);
Try it

[Edit]

function Mammal() {}
function Dog() {}
Mammal.prototype.walk = function () {
    console.log( 'walking..walking....' );
};
Dog.prototype.bark = function () {
    console.log( 'wof wof!' );
};

[/Edit]

let describe = Object.getOwnPropertyDescriptor;
let descriptor = Object.keys( Mammal.prototype ).reduce( function ( desc, key ) {
    desc[key] = describe( Mammal.prototype, key );
    return desc;
}, {} );
Dog.prototype = Object.create( Dog.prototype, descriptor );

[Edit]

Dog.prototype.constructor = Dog;
let dogInstance = new Dog();
dogInstance.walk();
dogInstance.bark();

Or use Object Inheritance follow this

function Mammal( key ) { this.key = key; }
function Dog() {
    Mammal.call( this, "Dog" );
}
Dog.prototype = Object.create( Mammal.prototype );
Dog.prototype.bark = function () {
    console.log( 'wof wof!' );
};
Mammal.prototype.walk = function () {
    console.log( 'walking..walking....' );
    return `${this.key} from Mammal.`;
};
Dog.prototype.constructor = Dog;
let dogInstance = new Dog();
dogInstance.walk();
dogInstance.bark();

Compare dogInstance

 Mammal.prototype.isPrototypeOf( dogInstance );
 Dog.prototype.isPrototypeOf( dogInstance );

It's work for me :(

[/Edit]

Rajib Chy
  • 800
  • 10
  • 22
0

I think there is something wrong when resetting the constructor.

No, there's not.

Your problem is that you do

Dog.prototype = Object.create(Mammal.prototype);

after having created the bark method on the old Dog.prototype object - which you are overwriting with a new, empty object. Just do it before any assignments:

function Mammal() {
}
Mammal.prototype.walk = function(){
    console.log('walking..walking....');
};

function Dog() {
}
Dog.prototype = Object.create(Mammal.prototype); // do this before creating properties
Dog.prototype.constructor = Dog;
Dog.prototype.bark = function() {
    console.log('wof wof!');
};
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
-1

I'm not completely sure about this, but this line is the problem:

Dog.prototype = Object.create(Mammal.prototype);

Apparently you can't give Mammal prototype directly to Dog prototype

function Mammal() {
}
function Dog() {

}
Mammal.prototype.walk = function () {
 console.log( 'walking..walking....' );
}
Dog.prototype.bark = function () {
 console.log( 'wof wof!' );
}

Dog.prototype.mammal = Object.create( Mammal.prototype );
Dog.prototype.constructor = Dog;
let dogInstance = new Dog();
dogInstance.mammal.walk();
dogInstance.bark();

this seems to work

Rajib Chy
  • 800
  • 10
  • 22
Gabbr Issimo
  • 111
  • 9
  • Running your fix on his snippet doesn't seem to solve the problem. Now it doesn't even recognize the `walk ` function. – Lev Buchel Oct 14 '18 at 13:08
  • 1
    ok...i'm trying on this fiddle https://jsfiddle.net/3bv7nw2g/ and updated the answer...did you modify this line ? dogInstance.mammal.walk(); – Gabbr Issimo Oct 14 '18 at 13:09