We're all familiar with the following polyfill to create objects in JavaScript:
function inherit(C, P) {
var F = function () {};
F.prototype = P.prototype;
C.prototype = new F();
}
One downside of this is that you will not have access to the parent's own members. Child instances will only have access to the prototype chain.
So I think the best inheritance solution is as follows:
var inherit = (function () {
var F = function () {};
return function (C, P) {
F.prototype = P.prototype;
C.prototype = new F();
C.prototype.constructor = C;
}
}());
function Parent(a){
this.a = a;
}
Parent.prototype.a_proto = function(){return "a_proto";}
function Child(a,b){
Parent.apply(this,[a]);
this.b = b;
}
inherit(Child, Parent);
var c = new Child("a","b");
console.log(c.a); // a
console.log(c.b); // b
console.log(c.a_proto()); // a_proto
delete c.a;
console.log(c.a); // undefined
The reason why I like the above solution is because first you use the apply
function to copy the parent's own members to the child's own members, therefore allowing true inheritance of the parent's own members. Second, the child also has a link to the prototype chain, and can therefore inherit prototype properties of the parent…and since we use a proxy, the child cannot modify the parent's prototype.
I was also introduced to the concept of an uber method in the book JavaScript Patterns. Basically, in the uber method the author says we "add a reference to the original parent". This is like having access to the superclass in other languages and could be handy on occasion." Here's the implementation:
function inherit(C, P) {
var F = function () {};
F.prototype = P.prototype;
C.prototype = new F();
C.uber = P.prototype;
}
I don't see how adding a reference to the original parent is good. Now the child is able to modify the parent's prototype, which is a bad thing. What is this person talking about, we now have access to superclass like in other languages? I thought using apply
and the prototype chain already has given access to the superclass. Are there really any benefits to having a reference to the parent's prototype?