Code obtained from MDN :
function Employee() { this.name = ""; this.dept = "general"; } function Manager() { Employee.call(this); this.reports = []; } Manager.prototype = Object.create( Employee.prototype ); function WorkerBee() { Employee.call(this); this.projects = []; } WorkerBee.prototype = Object.create( Employee.prototype ); var x = new WorkerBee(); console.log( x );
Exit: WorkerBee {name: "", dept: "general", projects: Array[0]}
What effect does Employee.call(this); ? I know that starting code that is necessary for inheritance will succeed. The docs for .call() just indicate
the method calls the function with the given value and the arguments separately.
OK, so it calls the Employee() constructor, but it does not use the new operator, not return , which returns the object and its properties. How does Employee.call(this) force a child to inherit its parent properties?
If this line is not specified, only the projects array is present as a property.
source share