Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is technical review on MDN article about "inheritance" needed?

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 mark object inherits values for the name and dept properties from the prototypical object in mark.__proto__. It is assigned a local value for the projects property by the WorkerBee constructor.

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?

like image 738
overexchange Avatar asked Dec 05 '25 03:12

overexchange


1 Answers

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.

like image 69
a better oliver Avatar answered Dec 09 '25 21:12

a better oliver



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!