Could someone explain the difference between following inheritance implementations:
function Parent(name) {
this.name = name;
this.parentFoo = function() {
//...
}
}
function Child() {
this.additional = true;
Parent.apply(this, arguments); // Is it for copying parent fields to child object?
}
Child.prototype = Object.create(Parent.prototype);
and
function Parent(name) {
this.name = name;
}
Parent.prototype.parentFoo = function() {
//....
}
function Child() {
this.additional = true;
Parent.apply(this, arguments); // Is it for copying parent fields to child object?
}
Child.prototype = Object.create(Parent.prototype);
I mean what would be better to use, adding function to constructor directly or to prototype? I'm asking because
var childObj = new Child("Child name");
var parentObj = new Parent("Parent name");
childObj.foo();
parentObj.foo();
work well in both cases. Thank you!