Sub topic 8
Constructor Functions and the prototype Property
This chapter assumes you know how the [[Prototype]] link and the prototype chain work, and that you can inspect a prototype with Object.getPrototypeOf. So far you have set prototypes by hand with Object.create. Constructor functions do the same thing for you each time you build a new object.
The Problem Constructors Solve
Suppose you need many Person objects that share one greet method. With Object.create you would write a prototype object, call Object.create for every person, and then assign the fields by hand. A constructor function combines those steps. It is an ordinary function that you call with new, and by convention its name starts with a capital letter.
function Person(name) {
this.name = name;
}
Person.prototype.greet = function () {
return `Hi, I'm ${this.name}`;
};
const ada = new Person('Ada');
console.log(ada.greet()); // "Hi, I'm Ada"
console.log(Object.getPrototypeOf(ada) === Person.prototype); // true
console.log(Person.prototype.constructor === Person); // true
console.log(ada instanceof Person); // trueInside the constructor, this is the new object, so this.name = name creates an own property on each instance. The greet method lives in one place, Person.prototype, and every instance finds it by walking up the prototype chain.
Two Different Things Called "Prototype"
Most confusion about prototypes starts here. The word "prototype" refers to two separate things:
| Term | Belongs to | What it is |
|---|---|---|
| `[[Prototype]]` | Every object | The hidden link that property lookup follows. You read it with `Object.getPrototypeOf(obj)`. |
| `.prototype` | Functions | An ordinary property holding the object that becomes the `[[Prototype]]` of instances created with `new`. |
Person.prototype is not the prototype of Person. The prototype of Person is Function.prototype, because Person is a function. Person.prototype is the object that new Person() gives to each instance as its [[Prototype]].
function Person(name) {
this.name = name;
}
console.log(Object.getPrototypeOf(Person) === Function.prototype); // true
console.log(Object.getPrototypeOf(Person) === Person.prototype); // false
const ada = new Person('Ada');
console.log(Object.getPrototypeOf(ada) === Person.prototype); // trueCommon Errors
- Putting methods inside the constructor. If you write
this.greet = function () {...}in the constructor, every instance gets its own copy of the function. Put shared methods onPerson.prototype. - Calling without
new.Person('Ada')runs as a plain function call. In strict modethisisundefined, so the assignment throws a TypeError. In sloppy mode the call writesnameonto the global object. - Looking for
.prototypeon instances.ada.prototypeisundefined. Only functions get a.prototypeproperty automatically. UseObject.getPrototypeOf(ada)instead. - Arrow functions as constructors. Arrow functions have no
.prototypeproperty, and calling one withnewthrows a TypeError.
Exercise
Given function Car() {} and const c = new Car();, what do Object.getPrototypeOf(Car) === Car.prototype and Object.getPrototypeOf(c) === Car.prototype print, and why?
Show answer
The first prints false, because Car is a function and its [[Prototype]] is Function.prototype. The second prints true, because new sets the instance's [[Prototype]] to the constructor's .prototype property.
Exercise
A teammate defines this.describe = function () { return this.model; } inside a Car constructor. What does each instance cost, and how would you fix it?
Show answer
Each instance gets its own separate copy of the describe function. Move it to Car.prototype.describe = function () { return this.model; }; so that all instances share one function through the prototype chain.