liveBook · Manning

Constructor method

As explained in Secrets of the JavaScript Ninja, Third Edition

A JavaScript function used to create and initialize an object, typically when invoked with the new operator.

Need several objects with the same starting properties? A constructor initializes each new object. In a JavaScript class, the `constructor` keyword marks the function run by `new ClassName(...)`. Outside a class, an ordinary function can also serve as a constructor: what matters is how it is invoked. section 9.4.1 section 6.2.3

Initializing an instance

class Ninja { constructor(name, level) { this.name = name; this.level = level; } swingSword() { return true; } } const ninja = new Ninja("Yoshi", 4); `name` and `level` belong to this instance. `swingSword` belongs to the class’s prototype and is inherited by instances. section 9.4.1

Calling a constructor with `new` has three key effects:

  • It creates an object and supplies that object as `this` while the constructor runs. section 6.2.3
  • It sets the object’s prototype to the constructor function’s `prototype`, making prototype methods available through inheritance. section 9.3.2
  • It yields the constructed object unless the constructor explicitly returns another non-primitive value. section 6.2.3

The same function called without `new` is an ordinary function call, so do not assume it receives a fresh object as `this`. A class can omit an explicit constructor; an empty one is enough when no initialization is needed. Classes also define the constructor function and prototype together. In TypeScript, class syntax is usually easier to type-check than modifying an ordinary function’s prototype. section 6.2 section 9.4.1 section 9.3.2

Figure 9.6 Every function, when created, gets a new prototype object. When we use a function as a constructor, the constructed object’s prototype is set to the function’s prototype.

The prototype diagram shows the link established between a constructor function and an object created with `new`. section 9.3.2

How Secrets of the JavaScript Ninja, Third Edition covers it

How deep it goes
Works through object creation, `this`, return values, and prototype inheritance, then compares function constructors with class syntax.
Examples
Shows a function that assigns an instance method and a class that initializes fields while inheriting a prototype method.
What it assumes
Assumes familiarity with functions and calls; introduces constructor-specific `this` binding before adding prototype inheritance.
What you can do after
Build it from scratch with `new` and a constructor, or use class syntax to initialize instances and share prototype methods.
How it connects
Connects constructors to function invocation, `this`, prototypes, classes, and `instanceof`.

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 Secrets of the JavaScript Ninja, Third Edition. The section links point to the parts of the book it draws on.