liveBook · Manning

New keyword

As explained in JavaScript in Depth

A JavaScript operator that creates an object, links its prototype, invokes a constructor with that object as this, and determines the expression result from the constructor return value.

The expression new Gadget('phone') is not merely an instance factory. It performs four observable operations: allocates an object, links its prototype, invokes Gadget with that object as this, and decides whether the constructor's return value replaces it. section 6.1.1

Reason about new in this order:

  • Create a new ordinary object. section 6.1.1
  • Set the object's internal [[Prototype]] to the constructor's current prototype property. section 6.1.1
  • Call the constructor with the new object bound as this, so assignments such as this.name = value affect that object. section 6.1.1
  • Return the created object unless the constructor returns an object; primitive returns and no return value leave the created object as the result. section 6.1.1
function simulateNew(Constructor, ...args) {
  const obj = Object.create(Constructor.prototype);  #A
  const result = Constructor.call(obj, ...args);  #B
  return result instanceof Object ? result : obj;  #C
}
const g1 = new Gadget('phone');  #D
const g2 = simulateNew(Gadget, 'phone');  #D

A compact simulation exposes the mechanism: Object.create establishes the prototype, Constructor.call supplies this, and the final test selects the expression result. section 6.1.1

When the constructor replaces the result

A constructor that returns { b: 2 } discards the initialized this object, while one that returns 42 does not. Returning a cached object or Proxy is possible, but uncommon. section 6.1.1

new.target distinguishes construction from an ordinary call: it is undefined without new and refers to the called function with new. Classes enforce construction automatically, while older constructor-style APIs may guard or redirect calls themselves. section 6.1.2

Prototype choice is time-sensitive: changing Constructor.prototype affects later instances, not objects already created. A null prototype therefore produces an object inheriting from nothing. section 6.1.1

How JavaScript in Depth covers it

How deep it goes
Traces four construction steps and return selection, then stops short of detailed runtime implementation beyond internal prototype and binding concepts.
Examples
Uses Gadget, simulated construction, object-returning constructors, primitive-returning constructors, and new.target logging.
What it assumes
Assumes familiarity with functions and objects while establishing prototype linkage, constructor calls, and construction-time this behavior.
What you can do after
Diagnose unexpected instances by tracing prototype linkage, constructor this, return replacement, and construction detection.
How it connects
Connects constructor functions with prototypes, this binding, new.target, classes, and constructor return values.

Also explained in

Compare how 4 books explain new keyword →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of JavaScript in Depth. The section links point to the parts of the book it draws on.