Sub topic 4
What Classes Really Are: Functions and Prototypes
This chapter assumes you can write a basic class with a constructor and methods, as covered in the previous chapters. It also assumes you know that JavaScript objects have properties you can read with dot notation.
The class keyword did not add a new object model to JavaScript. A class is a constructor function, and its methods are stored on a shared prototype object. Once you see this, behavior such as method sharing and instanceof follows from it.
A Class Is a Function
When JavaScript evaluates a class declaration, it creates a function named after the class. The constructor body becomes that function's body. Every function created this way has a prototype property. That property holds an object, and each instance made with new links to that object.
class Point {
constructor(x, y) {
this.x = x;
this.y = y;
}
distanceTo(other) {
return Math.hypot(this.x - other.x, this.y - other.y);
}
}
const p = new Point(1, 2);
console.log(typeof Point); // "function"
console.log(Object.getPrototypeOf(p) === Point.prototype); // true
console.log(Object.hasOwn(p, 'distanceTo')); // false
console.log(Object.hasOwn(Point.prototype, 'distanceTo')); // trueEach line checks one part of the model:
typeof Pointis"function", so the class is a function value.- The instance's internal prototype is
Point.prototype. distanceTois not an own property ofp. Onlyxandyare, because the constructor assigned them tothis.distanceTolives onPoint.prototype, once, for all instances.
How Property Lookup Works
When you write p.distanceTo, JavaScript first checks p's own properties. If the name is not there, it follows the link to Point.prototype and checks there. If it is still not found, it moves on to Object.prototype, and then stops at null. This series of links is the prototype chain.
This explains why p.toString() works even though you never defined it. It also means a thousand Point instances share a single distanceTo function and do not each carry a copy.
The Same Thing Without class
Before ES2015, developers wrote the same structure by hand:
function OldPoint(x, y) {
this.x = x;
this.y = y;
}
OldPoint.prototype.distanceTo = function (other) {
return Math.hypot(this.x - other.x, this.y - other.y);
};
const q = new OldPoint(0, 0);
console.log(q.distanceTo(new OldPoint(3, 4))); // 5
console.log(Object.getPrototypeOf(q) === OldPoint.prototype); // trueThe resulting objects have the same shape. The class version is shorter and adds some safety rules, which the next chapter covers.
Common Mistakes
| Mistake | What actually happens |
|---|---|
| Expecting `Object.keys(p)` to list methods | Only own properties are listed, so you get `['x', 'y']` |
| Modifying `Point.prototype` at runtime | The change affects every existing and future instance |
| Confusing `Point.prototype` with `Object.getPrototypeOf(Point)` | The first is what instances link to; the second is the function's own prototype, `Function.prototype` |
Exercise
Given class A { hi() { return 'hi'; } } and const a = new A();, what do Object.hasOwn(a, 'hi') and a.constructor === A return, and why?
Show answer
Object.hasOwn(a, 'hi') returns false because hi is defined on A.prototype, not on the instance. a.constructor === A returns true: a has no own constructor property, so lookup finds A.prototype.constructor, which points back to A.
Exercise
You run Point.prototype.describe = function () { return (${this.x}, ${this.y}); }; after creating p = new Point(1, 2). Does p.describe() work?
Show answer
Yes, it returns "(1, 2)". p links to Point.prototype and does not hold a copy of it, so lookup finds the new method even though it was added after p was created.