function TodoList() {}
TodoList.prototype.items = [];
const list1 = new TodoList();
const list2 = new TodoList();
list1.items.push('Buy milk');
console.log(list1.items.length);
console.log(list2.items.length);function TodoList() {
this.items = [];
}
const list1 = new TodoList();
const list2 = new TodoList();
list1.items.push('Buy milk');
console.log(list1.items.length);
console.log(list2.items.length);Bug: Arrays on the prototype are shared by all instances. list1.items.push() mutates the single shared array, so list2 sees it too.
Explanation: Initializing items in the constructor creates a fresh independent array per instance.
Key Insight: Never put mutable state on a prototype. Always initialize arrays and objects in the constructor.