Skip to content

OutputNot attempted

Reassigning Fn.prototype after creating instances

function Animal(name) {
this.name = name;
}
Animal.prototype.speak = function () {
return `${this.name} speaks`;
};
const dog = new Animal('Rex');
Animal.prototype = {
speak() {
return 'replaced';
},
};
const cat = new Animal('Tom');
console.log(dog.speak(), cat.speak(), dog instanceof Animal, cat instanceof Animal);

Spaces and quote style don’t matter · write a thrown error as TypeError

Ctrl Enter
Show answer & explanation

The expected output comes from actually running the snippet; opening this shows it next to the explanation.

Reassigning Animal.prototype does not touch the old object; dog's [[Prototype]] still points to it → "Rex speaks". cat links to the new object → "replaced". instanceof asks "is the CURRENT Animal.prototype in your chain?" → dog false, cat true. Same idea as let a = obj; obj = {}.