Change the Prototype to a New Object - Trouble in understanding how prototype properties work

Tell us what’s happening:

  **Your code so far**
function Dog(name) {
	this.name = name;
}

Dog.prototype = {
	// Only change code below this line
	eat() {return "Eating"},
};

I have 2 questions

  • Why does this return false?
let doggy1 = new Dog("Ruff");
console.log(doggy1.constructor === Dog) // return false
  • Why do this works:
console.log(doggy1.eat());

but not this

console.log(doggy1.prototype.eat());

I thought since it’s a prototype property, it would make sense to have ‘prototype’ in their calls.

  **Your browser information:**

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

Challenge: Change the Prototype to a New Object

Link to the challenge:

The constructor property is actually defined inside the prototype so when you modify the Dog.prototype the constructor will also get deleted and have a default value of object. TO avoid this you can do like

Dog.prototype = {
	// Only change code below this line
        constructor : Dog;
	eat() {return "Eating"},
};

the prototype is only available on the Constructor function which is Dog here. You cannot access it on the object.

then you can do something like doggy1.__proto__.eat()

1 Like