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

















