OutputNot attempted
Prototype pattern with Object.create
const enemy = { hp: 100, attack() { return this.name + ' attacks'; },};const bug = Object.create(enemy);bug.name = 'Buggy';console.log(bug.attack(), bug.hp, Object.keys(bug));bug.hp -= 30;console.log(bug.hp, enemy.hp, Object.keys(bug));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.
Object.create links bug to enemy as its prototype: methods and defaults are shared, not copied. bug.hp -= 30 READS 100 from the prototype and then WRITES an own property 70; the prototype keeps 100. Prefer Object.create over setPrototypeOf, which deoptimises objects at runtime.