Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Classes  ›  Lesson

Class Syntax and Constructors

Classes 12 minA class is a blueprint: its constructor runs once per new instance to set that instance's own data, its methods are shared by every instance, and calling a class without new throws a TypeError.

Class Syntax and Constructors

In short: a class is a blueprint for building objects that share a shape. Its constructor method runs once per new instance, setting up that instance's own data with this.property = value. Every other method defined in the class body is shared — all instances use the exact same function, not a separate copy each. A class must always be built with new; calling it as a plain function throws. Class bodies are also always strict mode, whether or not you wrote "use strict" anywhere.

class Song with a constructor(title, sec) setting this.title/this.seconds, and a shared describe() method, with arrows to three instances — new Song("Blue Hour", 214), new Song("Red Dawn", 198), new Song("Green Tide", 230) — every instance gets its own data but shares the SAME describe() method; calling Song("x") without new throws

Build two songs, and compare their data and methods

Each new Song(...) call runs the constructor fresh, giving that instance its own properties — but all instances point at the exact same method:

javascript

a.describe === b.describe is true — describe is defined once, on the class itself, and every instance shares that single function. a.title === b.title is false — this.title = title in the constructor gives each instance its own, separate property, set from whatever was passed to new Song(...). a.describe() reads this.title and this.seconds off a specifically, printing "Blue Hour (214s)".

The trap: calling a class without new

A class constructor refuses to run as an ordinary function call — new is mandatory, and JavaScript enforces it with a thrown error rather than silently doing the wrong thing:

javascript

This throws TypeError: Class constructor Song cannot be invoked without 'new'. Regular functions can be called either way (fn() or new fn()) with different but valid behaviour for each; classes deliberately close off the plain-call path entirely.

Quick practice

quick drill

Given two instances of the same class, are their shared methods the exact same function?

quick drill

Given class Song { constructor(title) { this.title = title; } }, are new Song("a").title and new Song("b").title the same value?

quick drill

What happens when you call a class as a plain function, like Song("Blue Hour"), without new?

quick drill

Is a class body strict mode, even without writing "use strict"?

quick drill

What does a class's constructor method do?

🎯 Your turn

Given class Song { constructor(title, seconds) { this.title = title; this.seconds = seconds; } describe() { return \${this.title} (${this.seconds}s)`; } }, explain what happens when you run const a = new Song("Blue Hour", 214); const b = new Song("Red Dawn", 198); console.log(a.describe === b.describe); console.log(a.title === b.title);, and why Song("x", 1)withoutnew` fails.

Write your explanation and submit it — there's no code to run here, just your reasoning.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainOwn data, shared behaviour

Given class Song { constructor(title, seconds) { this.title = title; this.seconds = seconds; } describe() { return ${this.title} (${this.seconds}s); } }, explain what happens when you run const a = new Song("Blue Hour", 214); const b = new Song("Red Dawn", 198); console.log(a.describe === b.describe); console.log(a.title === b.title);, and why Song("x", 1) without new fails.