new operator and function constructor in Javascript
Constructor function
- The constructor function can be created with a new keyword
- When a new operator is used to create a function the following things happen
How it works
- The new empty object is created
- The function is called and this keyword is set
- An empty object is linked to a prototype
- The function returns the empty object
let Employee = function (name, department) { console.log(this); this.name = name; this.department = department; console.log(this); } let john = new Employee('John', 'Software'); console.log(john); // Employee {} // Employee { name: 'John', department: 'Software' } // Employee { name: 'John', department: 'Software' }