Classes
A JavaScript class is syntax for a constructor function plus methods on its prototype. Instances share those methods; each instance has its own fields.
Detailed Explanation
class did not add a new object model. It wrapped the one JavaScript already had: a constructor, a .prototype object, and instances linked to that prototype. new User("Ada") still creates an object, sets its [[Prototype]] to User.prototype, runs the constructor with this bound to the object, and returns it. What class adds is stricter rules and clearer syntax.
A class body is always strict. A class declaration is hoisted into the temporal dead zone, like let: you cannot call it above its line. The class name is local to the class body as well, so methods can refer to it.
The constructor method is the function that new actually invokes. If you omit it, you get a default empty constructor (or one that calls super(...args) in a subclass). There can be only one. Returning an object from constructor replaces the instance; returning a primitive is ignored.
Methods written in the class body go on User.prototype, not on each instance. That is why a thousand users share one describe function. this inside a prototype method is the instance, when you call user.describe(). Extracting the method (const fn = user.describe; fn()) loses that receiver, same as any other function.
Public fields (count = 0) are installed on each instance at construction time, not on the prototype. They run after super() in a subclass. Private fields (#count = 0) are also per-instance and are invisible outside the class. Static methods and fields hang off the constructor itself (User.idFromEmail), not off instances.
Class expressions exist: const C = class { ... } and const C = class Name { ... }. export default class {} is valid. Unlike function, there is no hoisted usable class before the line.
What it costs you: thinking a class is a Java class. There is no built-in overloading, no implicit this binding for extracted methods, no privacy except # fields, and inheritance is still the prototype chain. Engines optimise classes well when the shape of instances stays consistent. Adding extra properties later is allowed and slower.
Reach for a class when a type has identity, shared methods, and maybe a hierarchy. Reach for a factory function plus a closure when you want privacy without # or a prototype. Reach for a plain object when you have one value, not a type.
Code Examples
class User {
constructor(email) {
this.email = email;
this.createdAt = Date.now();
}
describe() {
return this.email;
}
}
const ada = new User("ada@example.com");
console.log(ada.describe()); // => ada@example.com
console.log(ada instanceof User); // => true
console.log(Object.hasOwn(ada, "describe")); // => false
console.log(typeof User.prototype.describe); // => functionclass Counter {
count = 0; // own property on each instance
inc() {
this.count += 1;
return this.count;
}
}
const a = new Counter();
const b = new Counter();
a.inc();
console.log(a.count, b.count); // => 1 0try {
User;
} catch (err) {
// if this line were above the class User declaration, this would be ReferenceError
}
const Fn = User;
const other = new Fn("other@example.com");
console.log(other.email); // => other@example.comCommon Gotchas
Calling a class without `new` throws
`User()` is a `TypeError`. Ordinary constructor functions do not throw; classes do.
Methods are not bound
passing `ada.describe` to `map` or `setTimeout` loses `this`. Use `ada.describe.bind(ada)` or a public field arrow (`describe = () => this.email`).
Class declarations are in the TDZ
unlike `function User() {}`, you cannot instantiate above the `class` line.
Fields are per instance, methods are on the prototype
a public field that holds an arrow is a new function per object. That costs memory and is the usual way to auto-bind `this`.