Start here.
A property lookup begins on the object itself. If the property is missing, JavaScript can follow a link to another object called its prototype and search there. That linked object can supply shared methods. This is the prototype chain. It allows many individual objects to use the same function without storing a separate copy of that function on each one.
A class is syntax for a common pattern of creating related objects. class Counter { constructor() { this.value = 0; } next() { return ++this.value; } } describes how a Counter is made and what it can do. new Counter() creates an instance and runs constructor. The assignment this.value = 0 gives that instance its own count. The next method is shared through Counter.prototype, yet this inside next refers to the instance receiving the call.
Two instances can therefore have separate values while using one shared method definition. Class syntax does not turn JavaScript into a different kind of language; objects and prototypes still do the underlying work. A plain object literal is enough for one record of data. A class becomes useful when you need multiple related instances with their own state and common behavior.
Build the model.
When a property is not on an object, JavaScript may look up its prototype chain. That delegation lets many instances share methods. class syntax provides a structured way to create instances and define shared behavior, but the underlying objects still use prototypes.
An instance's own data is separate from shared methods on the prototype. Use classes when instances have a stable identity and behavior that belongs together. For plain records, an object literal is often simpler. Composition can express shared behavior without a deep inheritance tree.
How it works.
When you read object.property, JavaScript first checks the object's own properties. If it is absent, it follows the object's prototype and repeats the search. A method on a class prototype can therefore be shared by every instance rather than copied onto each one. This lookup chain is delegation: the instance can use behavior supplied by another object.
A constructor runs when you call new Counter() and gives each new instance its own value property. The next method is placed on Counter.prototype. When counter.next() is called, the method is found through the prototype but this is the counter instance, so it updates that instance's value. Use class for a meaningful collection of instances; simple data records do not need one.
Read it line by line.
class Counter {
constructor() { this.value = 0; }
next() { return ++this.value; }
}First notice. Each Counter gets its own value; instances share the next method.
new Counter() creates an instance and runs constructor, assigning its value to zero. The instance has no own next property, so lookup reaches the prototype method. Calling that method as instance.next() makes this the instance; ++this.value changes its value to one and returns one. A second instance gets a separate value.
See the rule again.
class Counter {
constructor() { this.value = 0; }
next() { return ++this.value; }
}
const a = new Counter();
const b = new Counter();
console.log(a.next(), a.next(), b.next());The output is 1, 2, 1. Both objects find next on the shared prototype, but each call uses its own instance as this. The value property belongs to each instance, so b does not inherit a's count.
One thing to remember.
A class organizes creation and delegation; it does not change what an object is.