OutputNot attempted
MUTATING the prototype instead
function Animal(name) { this.name = name;}Animal.prototype.speak = function () { return `${this.name} speaks`;};const dog = new Animal('Rex');Animal.prototype.speak = function () { return `${this.name} barks`;};console.log(dog.speak(), Object.hasOwn(dog, 'speak'), 'speak' in dog);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.
Mutating the shared prototype object is visible to every existing instance. speak is not an own property of dog (it lives on the prototype), but in walks the chain.