class
Defines a class declaration or expression.
Classes provide a clean syntax for creating object instances with shared methods.
class Counter {
count = 0;
next() {
this.count += 1;
return this.count;
}
}
Objects, Prototypes & Classes
A class defines a blueprint for instances. The constructor initializes instance state, and methods are shared through the prototype.
Class syntax does not remove prototypes. It gives them a clearer shape. Understanding prototypes helps you understand what class methods really are.
Modern classes can use public fields, private fields and static methods. Use those tools when they make object boundaries clearer.
Defines a class declaration or expression.
Runs when new creates an instance.
Shared through the class prototype.
#count creates state only accessible inside the class body.
Examples
class Counter {
#count = 0;
next() {
this.#count += 1;
return this.#count;
}
}
class Helpers {
static format(value) { return value.trim(); }
static upper(value) { return value.toUpperCase(); }
}
// A simple module of functions may be clearer.
Code patterns
These patterns focus on the pieces developers actually reuse: object literals, methods, constructors, prototypes, getters, copying, classes and inheritance.
Initialize instance properties in a constructor.
class Record {
constructor(title) {
this.title = title;
}
}
const record = new Record("Overview");
Methods are shared between instances.
class Record {
constructor(title) { this.title = title; }
label() { return this.title; }
}
Use # for private class state.
class Counter {
#count = 0;
next() {
this.#count += 1;
return this.#count;
}
}
Call utility behavior on the class itself.
class Record {
static fromTitle(title) {
return new Record(title.trim());
}
constructor(title) { this.title = title; }
}
Rules that matter
Objects are where JavaScript programs start to model real application state. Good object shape makes later functions, classes and UI updates much easier to maintain.
Classes fit objects that share behavior and have state.
Set initial state; avoid hidden side effects.
#field is enforced by the language.
Do not expose every internal detail as public state.
Plain functions and objects are often simpler.
Detached methods can still lose context.
Production thinking
Classes make object creation more readable when many instances share the same behavior.
Class-based UI models should keep accessible state explicit and testable.
Production classes should have clear boundaries, small public APIs and minimal constructor side effects.
Content classes should render deterministic output and avoid hidden runtime-only dependencies.
Live code lab
The preview runs inside an isolated iframe. The JavaScript is placed inside the HTML editor for now, so every example stays together and remains easy to understand.
Mini assignment
Practice assignment
Try it yourself
Self-check
Object-oriented JavaScript becomes much easier when you can explain where the data lives, where the method is found and which object receives this.