Tell us what’s happening:
Describe your issue in detail here.
What is the difference between
Dog.prototype = Object.create(Animal.prototype); AND
Dog = Object.create(Animal.prototype); ?
**Your code so far**
function Animal() { }
Animal.prototype = {
constructor: Animal,
eat: function() {
console.log("nom nom nom");
}
};
function Dog() { }
// Only change code below this line
Dog.prototype = Object.create(Animal.prototype);
let beagle = new Dog();
**Your browser information:**
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Safari/537.36
Challenge: Set the Child’s Prototype to an Instance of the Parent
I’m not sure exactly what you’re asking here but let me see if I can help.
If you were to run this:
function Animal(){};
function Dog(){}; \\ option 1 - the way in question
function Bog(){}; \\ option 2 - the way from example
Animal.prototype = {
constructor: Animal,
eat: function() {
console.log("nom nom nom");
}
};
Then this:
Dog = Object.create(Animal.prototype);
You’ve created an instance of Animal
Dog instanceof Animal \\ output true.
If you want the “Child class to inherit from the Parent class” hence the word inheritance:
you would do this:
Bog.prototype = Object.create(Animal.prototype);
You can make an object called foo of the type Bog but you can’t make an Object of type Dog.
I’ve run some code in a codepen to hopefully make it more clear. You can see the outputs here: