Was going through Mozilla's Reference and came across this simple relationship.
function Employee() {
this.name = "";
this.dept = "general";
}
function Manager() {
Employee.call(this);
this.reports = [];
}
Manager.prototype = Object.create(Employee.prototype);
Apparantly, this forms a inheritance-like relationship from the Employee to the Manager.
Two questions here, Not sure what and why Employee.call(this); is there and
instead of assigning the object to Employee.prototype it is assigned to Manager.prototype. My thoughts were that Manager is inheriting from Employee, not vice versa. Perhaps this is the concept of the prototype chain where it really means both objects can grab properties from each other?
Would like some clarification.
Employee.prototype" - which object?Employee.prototype = Object.create(Manager.prototype);EmployeewithManagerthere. Right. But no, employee's prototype should not "have a manager object". TheEmployeeclass doesn't care which subclasses are extending it, and it won't hold references to them. If managers are inheriting from employees, then the prototype chain link goes fromManager.prototypetoEmployee.prototype, not the other way round.