Skip to content
Onsite

Output: changing a prototype property after creating an instance, then replacing the prototype object

MediumP2Output

What does this code print?

1function Dog() {}
2Dog.prototype.sound = 'woof';
3const d1 = new Dog();
4
5Dog.prototype.sound = 'bark';
6console.log(d1.sound);
7
8Dog.prototype = { sound: 'meow' };
9const d2 = new Dog();
10console.log(d1.sound, d2.sound);
11console.log(d1 instanceof Dog, d2 instanceof Dog);
Saved automatically

One line per console.log. Arrays as [1, 2, 3], objects as { a: 1 }, an uncaught error as Uncaught TypeError: message (Chrome and Node wording).

Hints

Saved automatically