Typorithm · medium

class is Sugar

class-vs-prototypeinterview-questionsclassextendssuper
Saved on this device
Full lesson · 8 learning steps Open any exercise for focused practice
1

Step 1 of 8 · intro card

Learn class is Sugar

0%

Prototypes & Classes

ES class syntax is sugar over prototypes. Methods in a class body live on Class.prototype and are shared, extends sets up the prototype chain between subclass and parent, and super calls the parent method. Under the hood it is still prototype-based.

A class declaration creates a constructor function plus a .prototype object. Methods you write in the class body are not copied onto each instance — they are placed on Class.prototype and shared by every instance via the prototype chain.

extends does the wiring: it sets the subclass's .prototype [[Prototype]] to the parent's .prototype, so Object.getPrototypeOf(Dog.prototype) === Animal.prototype. That single link is the entire inheritance mechanism.

Inside a subclass, super.method() reaches up to the parent's prototype to call its version, and super(...) in a constructor runs the parent constructor. None of this is new machinery — it is the prototype chain with friendlier syntax.

01

Class methods live on Class.prototype, shared across instances.

02

extends links Sub.prototype to Parent.prototype.

03

super reaches the parent method; it is all prototypes underneath.

class Animal {
  constructor(name) { this.name = name }
  speak() { return this.name + " makes a sound" }
}
class Dog extends Animal {
  speak() { return super.speak() + " (woof)" }
}
typeof Animal.prototype.speak === "function" // true — method is shared
Object.getPrototypeOf(Dog.prototype) === Animal.prototype // true

Read the implementation

Connect each part of the code to the concept before you type.

1. Methods live on .prototype

A method written in the class body is placed on Animal.prototype and shared by all instances.

speak() { return this.name + " makes a sound" }

2. extends links the chain

extends sets Dog.prototype's [[Prototype]] to Animal.prototype, building the inheritance chain.

class Dog extends Animal {

3. super calls the parent

super.speak() invokes the parent's method found on Animal.prototype.

super.speak()

4. Shared on the prototype

The method really lives on Animal.prototype, proving class methods are prototype-based and shared.

typeof Animal.prototype.speak === "function" // true — method is shared

5. Subclass chain

extends wires Dog.prototype's [[Prototype]] to Animal.prototype under the hood.

Object.getPrototypeOf(Dog.prototype) === Animal.prototype // true

A complete learning loop

01

Understand

Read the concept map and follow the code breakdown.

02

Reconstruct

Type the implementation, repair mistakes and work through every recall exercise.

03

Verify

Use the final check without hints; revisit weak concepts in short practice sessions.

class is Sugar: Lesson & Practice · Typorithm | ApplyDjinn