In the book Pro Javascript design patterns one of the ways for implementing inheritance is by using an extend function.
function extend(subClass, superClass) {
var F = function() {};
F.prototype = superClass.prototype;
subClass.prototype = new F();
subClass.prototype.constructor = subClass;
}
Sample usage
function Person(name) {
this.name = name;
}
Person.prototype.getName = function() {
return this.name;
}
function Author(name, books) {
Person.call(this, name);
this.books = books;
}
extend(Author, Person);
So, why can't the same function be implemented this way?
function extend(subClass, superClass) {
subClass.prototype.__proto__ = superClass.prototype
}
What is the difference between the two implementations if they are not the same?
__proto__was not supported by IE < 11 developer.mozilla.org/en/docs/Web/JavaScript/Reference/… among other things