Whats the difference between
Employee.prototype = Object.create(Person.prototype);
and
_.extend(Employee.prototype, Person.prototype);
Both give similar results (output), but the underscore method seems to add the Person.prototype to the Employee.constructor.prototype, and quite abit extra stuff here and there, why?
pure JS

underscoreJS

A nice side effect of _.extend is I can easily do multiple inheritance: seems like it doesnt make the prototype chain longer too …
_.extend(Employee.prototype, Person.prototype);
_.extend(Employee.prototype, {
doSomething: function() {
return "hi ...";
}
});
But …

Why is there 2 sayHi and doSomething functions? (actually its the same when I just do 1 extend).
With
Employee.prototype = Object.create(Person.prototype);you are completely replacing theEmployee.prototype.But with
_.extend(Employee.prototype, Person.prototype);you are adding thePerson.prototypeon top of theEmployee.prototype.For example,
As you see,
ait’s not completely replaced byb, it’s just extended by the properties defined inb.