Sub topic 10
The constructor Property
This chapter assumes you know how constructor functions and their prototype property work, what new does, and how property lookup walks the prototype chain.
A Back-Reference from Prototype to Function
Every ordinary function gets a prototype object when it is created. That object starts with one property, constructor, which points back to the function. This gives you a two-way link: the function refers to its prototype, and the prototype refers to the function.
Instances do not have their own constructor property. They inherit it through [[Prototype]].
function Person(name) {
this.name = name;
}
const ada = new Person('Ada');
console.log(Person.prototype.constructor === Person); // true
console.log(ada.constructor === Person); // true (inherited)
console.log(Object.hasOwn(ada, 'constructor')); // false
console.log(Object.getOwnPropertyDescriptor(Person.prototype, 'constructor'));
// { value: [Function: Person], writable: true, enumerable: false, configurable: true }The property is non-enumerable, so it does not appear in for...in loops or Object.keys.
Using constructor to Make Similar Objects
When you have an instance but not its constructor, constructor lets you build another object of the same kind:
function Person(name) {
this.name = name;
}
const ada = new Person('Ada');
const twin = new ada.constructor('Twin');
console.log(twin instanceof Person); // true
console.log(twin.name); // "Twin"Common Error: Losing constructor by Replacing the Prototype
Assigning a new object literal to prototype throws away the default object, and constructor goes with it. After that, lookup finds Object.prototype.constructor instead:
function Car() {}
Car.prototype = {
drive() {
return 'vroom';
}
};
const c = new Car();
console.log(c.constructor === Car); // false
console.log(c.constructor === Object); // trueTo fix it, put the property back with the same attributes the engine would have given it:
function Car() {}
Car.prototype = {
drive() {
return 'vroom';
}
};
Object.defineProperty(Car.prototype, 'constructor', {
value: Car,
writable: true,
configurable: true,
enumerable: false
});
const c = new Car();
console.log(c.constructor === Car); // trueYou can also avoid the problem by adding methods one at a time (Car.prototype.drive = ...) instead of replacing the whole object.
Don't Use constructor for Type Checks
The constructor property is an ordinary writable property, so any code can overwrite or delete it. The engine does not use it when new runs or when instanceof runs. Treat it as a convenience. For type checks, use instanceof or isPrototypeOf, which a later chapter covers.
| Check | Relies on | Can break if |
|---|---|---|
| `obj.constructor === F` | A writable data property | The prototype is replaced or `constructor` is overwritten |
| `obj instanceof F` | The prototype chain and `F.prototype` | `F.prototype` is reassigned after `obj` is created |
Exercise
After running function Box() {}, then Box.prototype = { open() {} };, then const b = new Box();, what does b.constructor evaluate to, and why? What does b instanceof Box return?
Show answer
b.constructor is Object. The replacement object literal has no own constructor, so lookup continues to Object.prototype.constructor. b instanceof Box is true, because b's [[Prototype]] is the current Box.prototype, and instanceof does not read constructor.
Exercise
Why is constructor absent from Object.keys(Person.prototype), even though Person.prototype.constructor exists?
Show answer
The engine creates constructor as a non-enumerable property, and Object.keys lists only own enumerable string keys.