Sub topic 9
What new Actually Does
This chapter assumes you know constructor functions and the prototype property from the previous chapter, and that you can call a function with a chosen this using call or apply.
When you write new Person('Ada'), JavaScript runs a fixed sequence of steps. If you know those steps, you can predict what any constructor call returns and work out why a call went wrong.
The Steps
When a function F is called with new, the engine does the following:
- It creates a new, empty object.
- It sets that object's
[[Prototype]]toF.prototype. IfF.prototypeis not an object, it usesObject.prototypeinstead. - It calls
Fwiththisbound to the new object and passes along the arguments. - If
Freturns an object, that object is the result. Otherwise, the new object from step 1 is the result.
Rebuilding new by Hand
You can write the same steps as an ordinary function:
function myNew(Constructor, ...args) {
const proto = Constructor.prototype;
const isObject = (v) =>
v !== null && (typeof v === 'object' || typeof v === 'function');
const obj = Object.create(isObject(proto) ? proto : Object.prototype); // steps 1-2
const result = Constructor.apply(obj, args); // step 3
return isObject(result) ? result : obj; // step 4
}
function Person(name) {
this.name = name;
}
Person.prototype.greet = function () {
return `Hi, I'm ${this.name}`;
};
const ada = myNew(Person, 'Ada');
console.log(ada.greet()); // "Hi, I'm Ada"
console.log(Object.getPrototypeOf(ada) === Person.prototype); // trueThe real new does a little more. For example, it refuses to run functions that cannot be constructors. For ordinary functions, though, myNew behaves the same way.
Common Errors
Returning an object from a constructor
Step 4 means an explicit object return replaces the instance. Primitive return values are ignored.
function Weird() {
this.a = 1;
return { b: 2 };
}
function Fine() {
this.a = 1;
return 42;
}
console.log(new Weird()); // { b: 2 } (not linked to Weird.prototype)
console.log(new Fine()); // Fine { a: 1 }Forgetting new
If you call a constructor without new, none of these steps happen. this is undefined in strict mode, so this.name = name throws a TypeError. In sloppy mode, this is the global object, so the constructor silently creates a global variable. You can guard against this with new.target, which is undefined when the function was called without new:
function Person(name) {
if (!new.target) {
throw new TypeError('Person must be called with new');
}
this.name = name;
}Using arrow functions as constructors
Arrow functions have no prototype property and no this of their own, so new on one of them throws a TypeError.
Summary
| Step | Effect |
|---|---|
| Create | A fresh empty object |
| Link | `[[Prototype]]` set to `F.prototype` |
| Call | `F` runs with `this` set to the new object |
| Return | The returned object if there is one, otherwise the new object |
Exercise
Given function Box(v) { this.v = v; return null; }, what does new Box(3) evaluate to, and is the result's prototype Box.prototype?
Show answer
null is not an object for this rule, so the return value is ignored. The result is the new object { v: 3 }, and its prototype is Box.prototype.
Exercise
Why does Object.getPrototypeOf(new Weird()) === Weird.prototype evaluate to false in the example above?
Show answer
Weird explicitly returns the object literal { b: 2 }, which replaces the instance created in step 1. That literal's prototype is Object.prototype, not Weird.prototype.