In JavaScript, the new keyword is used to create an instance of a constructor function or class. Here's how it works:
-
It creates a new, empty object.
-
It sets the prototype of the new object. The new object's __proto__ property is linked to the constructor function's prototype property, establishing the inheritance chain. This allows the new object to access properties and methods from the prototype.
-
It binds this to the new object. Inside the constructor function, this refers to the newly created object, enabling you to assign properties and methods to it.
-
It returns the new object. If the constructor function doesn't explicitly return a value, the new object is returned by default.