Prototype inheritance, subtype not inheriting all properties

I’m doing this lesson…

It say’s by setting my new objects .prototype to Object.create(parentObject.prototype) I will inherit all of parentObject’s properties and methods.

In the example it inherits the method just fine but I tried adding a property to the parent and that does not seem to be inherited and I don’t understand why. Surely inheritance isn’t all that useful if it only gives you the methods and not the properties they work on? What am I missing here? Example below…

    function Animal() {
        this.sound = "Woof";
     }
    Animal.prototype = {
        constructor: Animal,
        eat: function() {
            console.log(this.sound, "nom nom nom");
        }
    };
    function Dog() { }
    Dog.prototype = Object.create(Animal.prototype);
    let beagle = new Dog();
    beagle.eat(); // Should, It think, print "Woof nom nom nom"

for that you have to add a property or a method like below

Animal.prototype = {
        constructor: Animal,
        eat: function() {
            console.log(this.sound, "nom nom nom");
        },
        name:"Alfred"//You can add propety hereafter
    };

and then do the following to see it

console.log(beagle.name)

Hello!

The problem you’re facing is that the prototype of Animal is being override by this code:

Animal.prototype = {
  constructor: Animal,
  eat: function() {
    console.log(this.sound, "nom nom nom");
  }
};

Basically, it’s creating a new object. One way for it to define the required property sound, would be:

Animal.prototype = {
  constructor: Animal,
  eat: function() {
    console.log(this.sound, "nom nom nom");
  },
  sound: 'woof'
};

beagle.eat();
// woof nom nom nom

Another way:

function Animal(sound) {
  this.sound = sound;
}

Animal.prototype.eat = function() {
  console.log(this.sound, 'nom nom nom');
};

function Dog(sound) {
  Animal.call(this, 'Woof');
}

Dog.prototype = Object.create(Animal.prototype);
Dog.prototype.constructor = Dog;
let beagle = new Dog();
beagle.eat();
// Woof nom nom nom

Hope it helps :slight_smile:!

So there’s no point setting any properties at all in the function? Ishould just put them in the prototype if they are to be set for every instance or add them to the instances after they are created if they are to be individual?

1 Like

It will depend on how you define them :stuck_out_tongue:. The last example I gave you defines a property (you could define a method too) on the base prototype (Animal) that all children will inherit.

Properties on the function are just properties on a Function object. They don’t play any part in object creation. The prototype is an object that represents the template that is “copied” (not really copied) into the new instance.

This all would have been a lot less confusing if Javascript had simply made Object.create() an instance method instead of static, let alone the highly magical new keyword. But we got what we got.

1 Like

Yes. If you want all to inherit the properties then you must set it inside the Animal.prototype or if you just want the dog to show this behaviour then you must make a dog prototype object and add your properties inside it. So all dogs would have the behaviour and if possible any descendants that inherit from dog. Great point @chuckadams. Happy New Year to everyone.

Thanks, I understand that things defined on the prototype are inherited. I was asking about properties defined on the parent function. The exercise gave me the impression they will be inherited too but it seems clear now that they won’t.