الانتقال إلى المحتوى
FrontendJavaScript· 7 دقائق للقراءة

JavaScript Classes Are Still Prototypes Underneath

Methods live once on the prototype, extends links objects, and only fields belong to each instance.

ReadIn

مقال كل يوم يفرق معاك

21 نوفمبر 2026
  • كاتب موثّق
  • ES2025
محتويات المقال

A team adds 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

The belief: JavaScript classes work like Java or C# classes. Each instance carries its own copy of the class's methods, and extends copies the parent's members into the child.

It's easy to see why. The syntax was designed to look familiar: 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

MDN calls classes "special functions" that are built on prototypes, with some syntax and semantics of their own. 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

MDN's class reference says it directly: methods are defined on the prototype and shared by all instances. Fields are different: public instance fields exist on every created instance, as its own properties. Reading a property walks the : JavaScript looks at the object itself, then its prototype, then that object's prototype, until it finds the name or reaches the end.

JavaScript account.js
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 time

The comments show what Node 24 and Chromium print. extends 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

An in a field is a field like any other: an own property, created anew for each instance, and never placed on the prototype. MDN describes the timing: a base class adds its fields before its constructor body runs, and a subclass adds its fields right after 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.

Inside a field initializer, 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.

JavaScript buttons.js
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 function

With methods, both versions of handleClick 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

Classes are still functions and prototypes, but they add rules of their own. A class body always runs in . Calling a class without 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.

MythReality
Each instance gets its own copy of every methodMethods live once on Class.prototype; instances reach them through the chain
extends copies the parent's members into the childextends links Child.prototype to Parent.prototype; lookups walk the link
A class is a new kind of type, separate from functionstypeof a class is "function", and new builds an object linked to its prototype
Arrow-function fields are methods with a fixed thisThey're own properties: one function per instance, and nothing for super to find

How to apply it in your project

  1. Use methods for behavior and fields for dataPut behavior in methods, so it's shared and can be overridden with super. Keep fields for per-instance state.
  2. Keep arrow fields for handlers nobody overridesIf 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.
  3. Debug with the chain in mindWhen a property looks "missing" or "wrong", check Object.hasOwn and walk Object.getPrototypeOf to see which object actually answers the lookup.
  4. Leave built-in prototypes aloneNever add methods to Array.prototype, Object.prototype or other built-ins. Write a helper function or a subclass instead.

Key takeaways

  • A JavaScript class is a constructor function plus a prototype object; typeof a class is "function".
  • Methods are stored once on the prototype and shared; instances find them by walking the prototype chain.
  • extends links 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 super can't reach them.
  • Classes still add real rules (strict mode, new required, private fields) on top of prototypes.

📚 المصادر

  1. Inheritance and the prototype chain — MDN
  2. Classes — MDN
  3. Public class fields — MDN
  4. Using classes — MDN
  5. Object.getPrototypeOf() — MDN

التعليقات (0)