
JavaScript Classes Are Still Prototypes Underneath
- كاتب موثّق
- ES2025
محتويات المقال
SubmitButton extends Button and overrides handleClick so it calls super.handleClick() first, which keeps the click tracking from the base class. In Java or C#, this is textbook overriding. Here, the first click throws, in Chrome as in Node, TypeError: (intermediate value).handleClick is not a function. The base class clearly has a handleClick; it's right there in the source. The catch is in how it was written: as an arrow function assigned to a field.
The common misconception
extends copies the parent's members into the child.
class, constructor, extends, super, and TypeScript adds access modifiers and interfaces on top. From the outside, an instance seems to own everything: account.deposit() works, and "deposit" in account is true. And one popular pattern, the arrow-function field, really does give every instance its own copy, which reinforces the picture. The half-truth hides where things live.
What actually happens
A class is a constructor function with a prototype object
typeof Account is "function". When you call new Account(), JavaScript creates an object whose internal [[Prototype]] points at Account.prototype, then runs the constructor on it. Every instance shares that one prototype object.
Methods live once, on the prototype
class Account { balance = 0; // field: an own property of each instance deposit(amount) { // method: one function on Account.prototype this.balance += amount; }}class SavingsAccount extends Account {}const a = new Account();const s = new SavingsAccount();console.log(typeof Account); // functionconsole.log(Object.hasOwn(a, "balance")); // trueconsole.log(Object.hasOwn(a, "deposit")); // falseconsole.log(a.deposit === s.deposit); // true: one shared functionconst parent = Object.getPrototypeOf(SavingsAccount.prototype);console.log(parent === Account.prototype); // trueAccount.prototype.close = function () { return "closed"; }; // added laterconsole.log(s.close()); // closed: found through the chain at call timeextends didn't copy deposit into SavingsAccount; it set SavingsAccount.prototype's own prototype to Account.prototype. That's why a method added to Account.prototype after s was created still works on s: lookups happen when you call, so nothing was ever copied to go stale.
Why the button broke
super() returns. So Button first puts its arrow on the new object, then SubmitButton defines its own arrow under the same name and replaces it.
super refers to the base class's prototype, which MDN notes contains the base class's instance methods but not its instance fields. Button.prototype has no handleClick, so super.handleClick is undefined, and calling it throws.
class Button { handleClick = () => console.log("Button: track click");}class SubmitButton extends Button { handleClick = () => { super.handleClick(); console.log("SubmitButton: submit form"); };}new SubmitButton().handleClick();// TypeError: (intermediate value).handleClick is not a functionclass Button { handleClick() { console.log("Button: track click"); }}class SubmitButton extends Button { handleClick() { super.handleClick(); console.log("SubmitButton: submit form"); }}const button = new SubmitButton();const onClick = () => button.handleClick(); // keeps `this` for a listeneronClick();// Button: track click// SubmitButton: submit formhandleClick live on their prototypes, and super.handleClick() finds the one on Button.prototype. The arrow fields were there to keep this when the handler is passed around; a small wrapper at the place where you register the listener does the same job, as the this article earlier in this path shows.
Not only sugar, either
new throws TypeError: Class constructor Account cannot be invoked without 'new', and a class declaration can't be used before the line that declares it. Private fields such as #balance have, as MDN puts it, no trivial equivalent in constructor functions.
Class.prototype; instances reach them through the chain | |
extends copies the parent's members into the child | extends links Child.prototype to Parent.prototype; lookups walk the link |
typeof a class is "function", and new builds an object linked to its prototype | |
this | super to find |
How to apply it in your project
Use methods for behavior and fields for data Put behavior in methods, so it's shared and can be overridden with super. Keep fields for per-instance state.Keep arrow fields for handlers nobody overrides If a class is meant to be extended, don't write its overridable behavior as arrow fields. Wrap the call where you register the listener, or bind in the constructor, and keep the method itself on the prototype. Debug with the chain in mind When a property looks "missing" or "wrong", check Object.hasOwnand walkObject.getPrototypeOfto see which object actually answers the lookup.Leave built-in prototypes alone Never add methods to Array.prototype,Object.prototypeor other built-ins. Write a helper function or a subclass instead.
Key takeaways
A JavaScript class is a constructor function plus a prototype object; typeofa class is"function".Methods are stored once on the prototype and shared; instances find them by walking the prototype chain. extendslinks prototypes instead of copying members, so changes to a parent prototype show up in every subclass instance.Fields, including arrow-function fields, are own properties created for each instance, and supercan't reach them.Classes still add real rules (strict mode, newrequired, private fields) on top of prototypes.
التعليقات (0)