Skip to content

OutputNot attempted

Array on the prototype is shared

function Team() {}
Team.prototype.members = [];
const t1 = new Team();
const t2 = new Team();
t1.members.push('ann');
t2.members = ['bob'];
console.log(t1.members, t2.members, new Team().members);

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.

push MUTATES the single array that lives on the prototype (shared by all instances). Assigning t2.members creates an own property that shadows it. Classic bug: put per-instance state in the constructor.