1

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.

6
  • Manager will inherit Employee class's properties and will also have its own properties/methods. However Employee class won't inherit Manager's properties though. Commented Aug 23, 2016 at 18:14
  • "instead of assigning the object to Employee.prototype" - which object? Commented Aug 23, 2016 at 18:16
  • @Bergi Employee.prototype = Object.create(Manager.prototype); Commented Aug 23, 2016 at 18:16
  • @Aaron yes, that sets the employees' prototype to an object inheriting from the managers' prototype. Commented Aug 23, 2016 at 18:17
  • 1
    @Aaron: Oops, I missed that you swapped Employee with Manager there. Right. But no, employee's prototype should not "have a manager object". The Employee class 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 from Manager.prototype to Employee.prototype, not the other way round. Commented Aug 23, 2016 at 18:23

1 Answer 1

2
  1. Employee.call(this) is like calling Employee(), but instead of using it to create a new object, it instead modifies the current Manager. In this example, it sets name and dept in the Manager.
  2. This statement:

    Manager.prototype = Object.create(Employee.prototype);
    

    means that any attributes set like Employee.prototype.x = val will also be available in Manager.prototype; however if you override them in Manager.prototype, they will be overriden.
    You should also run Manager.prototype.constructor = Manager to make that property correct.

Sign up to request clarification or add additional context in comments.

12 Comments

Is it okay to think of Employee.call(this) as super() in Java?
I think it’s similar. (I don’t know Java though).
@Aaron: Yes, that's exactly what it does. It calls the parent constructor on the child instance to create and initialise the parent properties.
@JF: Object.create does not copy anything.
Is JF's answer wrong? His explanation was extremely clear to me. Not understanding the downvotes.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.