Sub topic 5
How Classes Differ from Functions: Strict Mode, TDZ, and new
This chapter assumes you know how function declarations are hoisted, what let and const do, and how a class compiles down to a constructor function plus a prototype (covered in the previous chapter). A class is a function, but it does not behave like a function declaration in three ways: its body always runs in strict mode, its name sits in the temporal dead zone until the declaration runs, and you can only call it with new.
Side-by-Side Comparison
| Behavior | `function Foo() {}` | `class Foo {}` |
|---|---|---|
| Hoisting | Hoisted and initialized, usable before its line | Hoisted but uninitialized (TDZ) |
| Strict mode | Only if you opt in with `"use strict"` | Always strict inside the class body |
| Calling without `new` | Allowed, runs as a plain function | Throws `TypeError` |
| Methods enumerable | Prototype methods you assign are enumerable | Class methods are non-enumerable |
The Temporal Dead Zone
A class declaration is hoisted the same way let is. The engine knows the name exists from the start of the scope, but the binding stays uninitialized until execution reaches the class line. Any access before that line throws a ReferenceError.
try {
new Foo();
} catch (err) {
console.log(err.name); // 'ReferenceError'
}
class Foo {}
console.log(typeof new Foo()); // 'object'This trips people up when they move code around. With function declarations you can call a helper near the top of a file and define it at the bottom. With classes, the definition has to run first. Class expressions (const Foo = class {}) follow the same rule because they are assigned to a const or let.
Strict Mode Is Always On
Everything inside a class body runs in strict mode, including the constructor, methods, getters, and field initializers. You don't need to write "use strict". Two effects show up often:
- Assigning to an undeclared variable throws a
ReferenceErrorinstead of quietly creating a global. - A method called without a receiver gets
thisasundefined, not the global object.
class Tally {
addTypo() {
totl = 1; // misspelled, never declared
}
whoAmI() {
return this;
}
}
const t = new Tally();
try {
t.addTypo();
} catch (err) {
console.log(err.name); // 'ReferenceError'
}
const detached = t.whoAmI;
console.log(detached()); // undefinedThe undefined value of this is why detached methods fail. A later chapter covers how to fix that.
new Is Required
A regular function can run as a constructor or as a plain call, so a forgotten new used to leak properties onto the global object without any error. Classes rule this out. Calling a class without new throws immediately.
class Point {
constructor(x, y) {
this.x = x;
this.y = y;
}
}
try {
Point(1, 2);
} catch (err) {
console.log(err.name); // 'TypeError'
}
console.log(new Point(1, 2).x); // 1Common Errors
- Defining a class after the code that uses it. Move the class above its first use, or into a module that you import.
- Relying on sloppy-mode globals. Code that worked in a plain function can throw once you move it into a class method. Declare every variable.
- Calling a class as a callback factory.
items.map(Point)throws. Useitems.map(([x, y]) => new Point(x, y)).
Exercise
This code throws. Name the error type, explain why, and give the smallest fix. const shape = new Circle(2); class Circle { constructor(r) { this.r = r; } }
Show answer
It throws a ReferenceError. Class declarations are hoisted but stay in the temporal dead zone until their line executes, so Circle is uninitialized when new Circle(2) runs. Move the class declaration above the line that creates the instance.
Exercise
A colleague turns function Logger() { this.lines = []; } into class Logger { constructor() { this.lines = []; } }. Old code calls const log = Logger(); and now crashes. What error appears, and what change fixes the caller?
Show answer
A TypeError, because a class constructor cannot be invoked without new. Change the caller to const log = new Logger();