Skip to content
engineering workbench.by StudyPort Academy
Learning path
Advanced20 minLESSON 22 / 34

Constructors & classes

01

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.

State and behavior in a class
PieceLives where?Purpose
constructor / constructor functionOn the creation pathInitialize a new instance
Instance fieldOn an individual objectStore that object’s state
Prototype methodOn the shared prototypeReuse behavior across instances
static methodOn the constructor/class itselfWork that does not need an instance receiver
extends / superBetween class prototype relationshipsReuse and specialize a real parent relationship
#private fieldWithin the class’s private field rulesRestrict 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.

02

Mental model

account

name

role

shared

role

Object.prototype

toString, hasOwnProperty

null

End of the chain

Check account for an own property named role.

01 / 03Follow [[Prototype]]

03

Live example

The share increment through their but keep separate value . Updating first leaves second at zero.

Opening editor…
LIVE PREVIEW
Console 0

Your console output appears here.

Saved on this deviceJavaScript · ES2022
04

Guided challenge

Honor each instance’s input

Initialize the instance from the start parameter, including its default when omitted.

Opening editor…
LIVE PREVIEW
Console 0

Your console output appears here.

Saved on this deviceJavaScript · ES2022

EXPECTED CONSOLE OUTPUT

3
0

Hints 0/2

    Use all hints or make 3 failed runs (0/3).

    05

    Free sandbox

    Make a variation, test an assumption, or break the example on purpose. This draft is saved separately.

    Opening editor…
    LIVE PREVIEW
    Console 0

    Your console output appears here.

    Saved on this deviceJavaScript · ES2022
    06

    Quiz

    A quick check for understanding. Retry as often as you like; your learning path stays open.

    01Where are ordinary class methods stored?
    02Does class syntax automatically bind instance methods?

    Ready to call this one understood?

    Mark this lesson complete to earn 200 XP. You can always revisit it.

    Sign in to save progress
    Next lesson