Set the Child's Prototype to an Instance of the Parent - Please help

Set the Child's Prototype to an Instance of the Parent - Please help
0

#1

Tell us what’s happening:
What’s wrong with my code? It wants a , when I have a . inbetween Dog and Prototype in Dog.prototype. This makes no sense to me.
Please help, thank you.

Your code so far


function Animal() { }

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

function Dog() { }

// Add your code below this line
let Dog.prototype=Object.create(Animal.prototype);

let beagle = new Dog();
beagle.eat();  // Should print "nom nom nom"

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/object-oriented-programming/set-the-childs-prototype-to-an-instance-of-the-parent


#2

Look at the example for the Bird.prototype. Do you see a let in front of it? VAR, LET, and CONST are only for declaring variables and not prototypes.


#3

Thank you very much! Randell, you seem experienced, tell me, does it happen often that you miss simple stuff like that even when you’re “professional” at coding? Feels like I get blind for mistakes in my own code because I’m trying so hard to spot them. :laughing:


#4

We are human, so we will make mistakes. The more experience you have as a coder (and making mistakes), the faster you will be able to recognize the mistakes when they occur.


#5

Hi! Thanks for your answer. I was wondering, can you please clarify the difference between

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

and

let terrier = Object.create(Animal.prototype);

The first one is a prototype, the second one is an object. But what is the difference between them? Both are objects, both can be used to create new instances, both redived from the same supertype. I’m just having troubles with this topic and had to start it all over again to grasp it all and this difference seems to be the key.


#6

In your line of code:

you are creating a variable terrier and setting it equal to a new object that would inherit prototype of Animal.

As far as I can interpret,

sets Dog’s prototype to that of Animal’s as well, but I’m assuming Dog is previously constructed up via a Constructor or Function and not a normal one line assignment statement as with terrier, hence the Capitalized first letter of Dog.

If you’re referencing Dadebayor’s initial code, when

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

is inside

function Dog() {}

you are not directly assigning the prototype to a variable you are creating at the moment. The line of code is being written here for future use when someone wants to create and initialize a variable of type Dog, as in the case of terrier.


#7

I didn’t construct Dog, that was it, thank you for clearing that up!