I\'ve been watching Douglas Crockford\'s talks at YUI Theater, and I have a question about JavaScript inheritance...
Douglas gives this example to show that \"Hoozit\" i
The other answers address this, but if you DO want to inherit the prototype, you can use some parasitic magic:
Object.prototype.inherit = function(p) {
NewObj = function(){};
NewObj.prototype = p;
return new NewObj();
};
// Paraphrasing of Nicholas Zakas's Prototype Inheritance helper
function inheritPrototype(subType, superType) {
var prototype = Object.inherit(superType.prototype);
prototype.constructor = subType;
subType.prototype = prototype;
};
Now you can replace the:
Hoozit.prototype = new Gizmo();
with
inheritPrototype(Hoozit, Gizmo);
It might not be worth the trouble unless you have a real big Gizmo constructor (the only win in my suggestion is that you don't have to call Gizmo's constructor to hook up the prototype). I have examples of many of these types of patterns in TDD JavaScript Examples.