The new keyword invokes a function in a special way. Functions invoked using the new keyword are called constructor functions.
So what does the new keyword actually do?
// In order to better understand what happens under the hood, lets build the new keyword
function myNew(constructor, ...arguments) {
var obj = {}
Object.setPrototypeOf(obj, constructor.prototype);
return constructor.apply(obj, arguments) || obj
}
What is the difference between invoking a function with the new keyword and without it?
function Bird() {
this.wings = 2;
}
/* invoking as a normal function */
let fakeBird = Bird();
console.log(fakeBird); // undefined
/* invoking as a constructor function */
let realBird= new Bird();
console.log(realBird) // { wings: 2 }