Constructors & classes
Understand the idea
Two are supposed to have independent state, but changing one updates the other. The state was placed on a shared instead of each instance.
Assembly instructions for an instance
A counter needs a current value and a way to increment it. Keeping those together makes it clear which data each operation belongs to. -oriented programming, often shortened to OOP, organizes work this way; it is one useful design choice, not a rule that every needs a class.
What new actually does
Calling a regular constructor with new creates an object, connects its prototype, calls the constructor with that object as this, and normally returns the object. An explicitly returned object can replace that result. Leaving off new on an old constructor function can therefore fail or write to an unintended .
A constructor function called with new receives a fresh object as this. That object's prototype is linked to the constructor's prototype, then the constructor initializes it. A class constructor serves the same creation role with clearer class syntax. Calling a class without new throws.
| Piece | Lives where? | Purpose |
|---|---|---|
| constructor / constructor function | On the creation path | Initialize a new instance |
| Instance field | On an individual object | Store that object’s state |
| Prototype method | On the shared prototype | Reuse behavior across instances |
| static method | On the constructor/class itself | Work that does not need an instance receiver |
| extends / super | Between class prototype relationships | Reuse and specialize a real parent relationship |
| #private field | Within the class’s private field rules | Restrict direct access to internal state |
Classes and shared methods
A class provides clearer syntax for prototype-based construction. Its constructor initializes instance fields, and ordinary methods are stored on the prototype. Classes are not a separate inheritance system. They also add real rules: class constructors require new, class bodies are strict, and class declarations cannot be used before initialization.
Class methods normally live on the prototype and are shared by instances. Fields assigned on this belong to each instance. Calling classes “sugar over prototypes” describes that shared lookup model, not perfect equivalence with every old constructor pattern: class bodies are strict, classes cannot be called without new, and they have their own initialization rules.
Inheritance is an optional relationship
An instance method receives the instance through a method call. Passing it as a can still lose that receiver; class syntax does not automatically bind methods. A static method belongs to the class itself and is useful for work that does not need one particular instance.
Keep each instance independent
extends connects inheritance and super reaches parent behavior. In a derived constructor, call super before using this. Favor small classes with clear responsibilities; sharing a few lines is not by itself a reason to build a long inheritance hierarchy.
Use inheritance when one kind of object can honestly stand in for another. Otherwise, passing a helper object into a class may keep the relationship easier to change. In the example, first and second share the increment method but do not share value. Incrementing first therefore leaves second at zero.
Mental model
account
name
shared
role
Object.prototype
toString, hasOwnProperty
null
End of the chain
Check account for an own property named role.
01 / 03Follow [[Prototype]]
Live example
The share increment through their but keep separate value . Updating first leaves second at zero.
Your console output appears here.
Guided challenge
Honor each instance’s input
Initialize the instance from the start parameter, including its default when omitted.
Your console output appears here.
EXPECTED CONSOLE OUTPUT
3 0
Hints 0/2
Use all hints or make 3 failed runs (0/3).
Free sandbox
Make a variation, test an assumption, or break the example on purpose. This draft is saved separately.
Your console output appears here.
Quiz
A quick check for understanding. Retry as often as you like; your learning path stays open.
Ready to call this one understood?
Mark this lesson complete to earn 200 XP. You can always revisit it.