liveBook · Manning

Constructor method

As explained in JavaScript in Depth

A JavaScript function conventionally invoked with new to initialize an object and establish its prototype relationship.

A constructor method is a JavaScript function conventionally invoked with new to initialize an object and establish its prototype relationship. The surprising part is that new is not a single opaque operation: it allocates an object, links its prototype, calls the function with this bound to that object, and then examines the return value. section 6.1.1

Reason about construction in this order:

  • Allocate an ordinary object. section 6.1.1
  • Set its internal [[Prototype]] to the constructor function's prototype property. section 6.1.1
  • Call the function with this bound to the fresh object, so assignments such as this.name = value become own properties. section 6.1.1
  • Return the fresh object unless the constructor returns another object; primitive returns do not replace it. section 6.1.1

Simulating new

function simulateNew(Constructor, ...args) { const obj = Object.create(Constructor.prototype); const result = Constructor.call(obj, ...args); return result instanceof Object ? result : obj; } function Gadget(name) { this.name = name; } const gadget = simulateNew(Gadget, 'phone'); This decomposition exposes why changing a constructor's prototype affects later instances, while earlier instances retain the prototype selected at creation time. section 6.1.1

Constructor functions also provide an optimization boundary. When properties are assigned in a consistent order, V8 can learn and reuse the resulting object shape; incrementally assembling objects leaves more shape transitions for the engine to discover. section 5.10.1

Class constructors retain the same prototype-based construction model while adding guardrails: classes require new, and class methods are shared through the prototype. new.target distinguishes constructor calls from ordinary calls for function-based APIs. section 6.2.4 section 6.1.2

How JavaScript in Depth covers it

How deep it goes
Descends through allocation, prototype linkage, this binding, return substitution, new.target, and hidden-class optimization.
Examples
Uses Gadget construction, simulateNew, returning objects, class inheritance, and consistent property initialization.
What it assumes
Assumes JavaScript functions and objects, while developing prototype linkage, this binding, and class construction mechanics.
What you can do after
Diagnose constructor behavior by tracing new, prototype selection, this binding, return substitution, and invocation checks.
How it connects
Connects constructors with new, prototypes, classes, inheritance, hidden classes, object shapes, and new.target.

Also explained in

Compare how 18 books explain constructor method →

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.