Below is the code,
function Employee() {
this.name = "";
this.dept = "general";
}
function WorkerBee() {
Employee.call(this);
this.projects = [];
}
WorkerBee.prototype = Object.create(Employee.prototype);
var mark = new WorkerBee;
where MDN says:
The
markobject inherits values for thenameanddeptproperties from the prototypical object inmark.__proto__. It is assigned a local value for theprojectsproperty by theWorkerBeeconstructor.
As per the above code, constructor WorkerBee is explicitly calling Employee.call(this); but not inheriting values from mark.__proto__.
Presence of line WorkerBee.prototype = Object.create(Employee.prototype); is irrelevant to create local values for properties name and dept in mark object.
Do you think the statement given in MDN is wrong in saying inherits values? Are the examples and imagination need to be reworked?
Prior to ES 5 you would have written WorkerBee.prototype = new Employee, because there was no Object.create. The statement would have been somewhat correct then.
If you read on then you will actually see a diagram that shows that old code, which also omits Employee.call(this);.
So the answer is you are right and they forgot to update parts of the article.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With