Object Oriented Programming - Reset an Inherited Constructor Property

Tell us what’s happening:
Is it only methods that can be inherited by an objects prototype?

  **Your code so far**
function Animal() {}
Animal.prototype = {
constructor: Animal,
name: "Bosco",
legs: 4,
tail: 1,
eat: function(){
  console.log("nom nom nom")
function Bird() { }
function Dog() { }

Bird.prototype = Object.create(Animal.prototype);
Dog.prototype = Object.create(Animal.prototype);

// Only change code below this line
Bird.prototype.constructor = Bird;
Dog.prototype.constructor = Dog;

let duck = new Bird();
let beagle = new Dog();
  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:103.0) Gecko/20100101 Firefox/103.0

Challenge: Object Oriented Programming - Reset an Inherited Constructor Property

Link to the challenge:

Well the short answer is no, you can also add properties to the object’s prototype. If there are some properties which need to be present on each instance of the object, then it makes perfect sense to add it to the prototype.

I’d also like to know why properties inherited from a prototype property isn’t displayed in the parent object even though it’s present. eg,

console.log(duck) it displays an empty object { }
console.log(duck.name) == Bosco

while isn’t it
{name: “Bosco”, legs: 4, tail: 1}

That’s the whole point of using prototypes.

This returns an empty object because you haven’t added any properties to the duck object itself.

And this is working because, you added a name property to the Animal.prototype object and since duck inherits all of the properties from that object , you can use it.

In simper terms, when this line runs, the compiler will first check if the duck object contains the property name and since it does not, the next place it will check is the duck object’s prototype.

This whole procedure is called the prototype chain and if you want to learn more about it, here’s a resource.

hope this helps! :smile:

I forgot to thank you earlier, I got carried away. Thank you so much.

hahaha! don’t worry about it. That’s what we are here for and let us know if you need help with anything else! :smile:

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.