Glossary / new semantics

new semantics

The new operator invokes a function’s construct behavior to create an instance. Conceptually, it allocates an object, links that object to the constructor’s prototype, calls the constructor with the new object as this, and returns the constructed object unless the constructor explicitly returns another object. Class constructors enforce construction; ordinary functions may also be constructable.

Construction rules

function Point(x, y) {
  this.x = x;
  this.y = y;
}
const point = new Point(2, 4);
Object.getPrototypeOf(point) === Point.prototype; // true

If a traditional constructor returns an object or function, that returned object replaces the allocated instance. A primitive return value is ignored. Derived class constructors must call super() before accessing this. new.target identifies that a call was made with construction syntax and can be used to reject direct base-class construction.

Arrow functions, concise methods, and async/generator functions are generally not constructable. Reflect.construct(Target, args, NewTarget) offers explicit control over target construction and the prototype source. Built-ins may have special construction behavior. Do not infer constructability solely from typeof, because it returns "function" for both constructable and non-constructable callables.

Interview questions

  1. What are the steps of new? Allocate, link prototype, invoke with new this, then select the return value.
  2. What if the constructor returns a primitive? It is ignored; the allocated instance is returned.
  3. What if it returns an object? The explicit object becomes the result.
  4. How check construction inside a function? Inspect new.target.
  5. Can every function be used with new? No; arrows and several method/function forms are not constructable.

References