Sub topic 13
Classes Are Built on Prototypes
This chapter assumes you know how constructor functions, the prototype property, and the new operator work, as covered in earlier chapters. ES2015 class syntax adds no new inheritance model. It gives you a cleaner way to write the same constructor-and-prototype pattern, plus a few stricter rules.
The Same Structure, Different Syntax
A class declaration creates a function. The constructor method becomes that function's body. Every other method in the class body goes onto the function's prototype object.
class Person {
constructor(name) {
this.name = name;
}
greet() {
return `Hi, I'm ${this.name}`;
}
}
const ada = new Person('Ada');
console.log(typeof Person); // "function"
console.log(ada.greet()); // "Hi, I'm Ada"
console.log(Object.getPrototypeOf(ada) === Person.prototype); // true
console.log(Object.hasOwn(ada, 'greet')); // false
console.log(Person.prototype.constructor === Person); // truegreet lives on Person.prototype, so every instance shares one copy and finds it through the prototype chain. The links are the ones you would get from writing function Person and assigning Person.prototype.greet.
Where Classes Differ from Constructor Functions
The structure matches the older pattern, but classes behave differently in a few ways:
| Behavior | Constructor function | Class |
|---|---|---|
| Calling without `new` | Runs, and `this` is wrong | Throws `TypeError` |
| Methods on the prototype | Enumerable if assigned | Non-enumerable |
| Strict mode | Opt-in | Always on in the class body |
| Use before declaration | Hoisted and usable | Temporal dead zone error |
class Counter {
increment() {}
}
console.log(Object.keys(Counter.prototype)); // []
try {
Counter();
} catch (e) {
console.log(e instanceof TypeError); // true
}Class methods are non-enumerable, so a for...in loop over an instance does not list them. With the older pattern you had to call Object.defineProperty to get the same result.
Class Fields Are Own Properties
Class fields don't go on the prototype. Each field is created on the instance when the constructor runs, the same as an assignment to this.
class Button {
label = 'OK';
onClick = () => this.label;
render() {
return this.label;
}
}
const b = new Button();
console.log(Object.hasOwn(b, 'label')); // true
console.log(Object.hasOwn(b, 'onClick')); // true
console.log(Object.hasOwn(b, 'render')); // falseThe prototype of a class is an ordinary object. You can inspect it and patch it at runtime. You can also mix in other objects, which a later chapter covers.
Exercise
Given class Box { size = 1; open() {} } and const x = new Box();, which of these are true: Object.hasOwn(x, 'size'), Object.hasOwn(x, 'open'), Object.hasOwn(Box.prototype, 'open'), Object.keys(Box.prototype).includes('open')?
Show answer
Object.hasOwn(x, 'size') is true because fields are created on the instance. Object.hasOwn(x, 'open') is false because methods live on the prototype. Object.hasOwn(Box.prototype, 'open') is true. Object.keys(Box.prototype).includes('open') is false because class methods are non-enumerable.
Exercise
Rewrite class Person { constructor(name) { this.name = name; } greet() { return this.name; } } as a constructor function so that it matches the class. Make greet non-enumerable.
Show answer
function Person(name) { this.name = name; } followed by Object.defineProperty(Person.prototype, 'greet', { value: function () { return this.name; }, writable: true, configurable: true, enumerable: false });. One difference remains: the function version can still be called without new.