How it works...

As we saw in the previous recipe, the class syntax creates an empty constructor function if none is defined. A constructor is a method that is called immediately after a class is instantiated. Here, we create a new Rocket instance as follows:

const saturnV = new Rocket('Saturn V');

This means that the name property is actually assigned before that rocket instance is returned from the new expression and assigned to saturnV.

In this recipe, we define the constructor. The context of the constructor function, the value of this, is the instance of the new object. Therefore, when we assign the name property of this, it is set on that new instance.

Get ECMAScript Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.