By the end of this lesson
Create independent objects with a class and update one instance.
Before you start
- Node.js 24 with a terminal and plain-text editor; the browser-only lesson is labelled separately.
- Complete lesson 11 or be comfortable with its examples.
Three words to know
- Class
- A definition used to create instances with related methods.
- Instance
- An object created from a class.
- this
- The receiver used by a method call.
1. Give each instance its own state
constructor initializes a new instance. With a direct call such as first.finishOne(), this refers to first. The assignment in the constructor creates per-instance state, so a second instance starts independently.
class Progress {
constructor() {
this.completed = 0;
}
finishOne() {
this.completed += 1;
}
}
const first = new Progress();
const second = new Progress();
first.finishOne();
console.log(first.completed, second.completed);
What you should see · example output
1 0
2. A method depends on how it is called
Passing first.finishOne by itself as a callback loses the receiver of that original call. A wrapper such as () => first.finishOne() preserves which object is meant. Later lessons use callbacks; do not assume this is permanently attached to a method’s original object.
3. A class is one option, not a requirement
For decoded JSON, plain objects and validation functions are often the simplest choice. A class can model stateful behaviour, but constructing an instance does not automatically validate every assigned property. Inheritance, private fields and advanced prototype design are beyond this foundations route.
YOUR TURN
A small check before you continue
Create three Progress instances, call finishOne twice on only the second, and predict their completed values.
Show a suggested answer
They are 0, 2 and 0. Each instance has its own completed property. Sharing one instance under several names would be different.
Sources and scope
Documentation and examples reviewed 2026-10-18. Provider screens, package versions and output wording can differ. Examples show expected outcomes; they are not a claim that OpenViST connected to your server or verified your setup.
