Question about prototypes

I’m on the part where I’m taught to use properties with constructors because otherwise it’s a waste of memory. I’ve been playing around with it a bit so I can see what exactly happens.

What I have experienced:
I can reach the properties in the prototype via my instances. But when I call an instance object, these prototype properties are not in my instances objects. Why? They’re supposed to be in there?

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

Challenge: Use Prototype Properties to Reduce Duplicate Code

Link to the challenge:

Can you show a bit of code explaining what you mean?

1 Like
function Dog(name, age){

 this.name = name;
 this.age = age;

}

let beagle = new Dog("Snoopy", 17);

Dog.prototype.legs = 4;

console.log(beagle.legs) //So here he gives me 4, so its in the object
console.log(beagle) //propertie legs doesn't exist in it



in real world the console return this:
Dog { name: ‘Snoopy’, age: 17 }

I think you don’t get passed due to wrong code. Don’t add another this.numLegs in the prototype, you just add a line with the prototype command. What they mean in the task is that the way you are doing above creates a lot of extra lines in a program. The Dog.prototyp.numLegs = 4; is good if all your instances has the same number of legs (Birds has always 2 and Dogs 4 legs). Got it?

1 Like

Thanks @potsdam.rene ! I passed the test, but wanted to understand the concept prototype well. Don’t just want to carry on when I don’t quite understand. you feel me :raised_hands:

SII ! I understand it ! So the prototype is usefull if all your instances have the same value. This was confusing because when I call the instance, I didn’t see the prototype property in it. Which also makes sense, you only see the ownproperties.

1 Like

Start from 7:35 , well explained