inherit.js is an easy way to properly use prototype chaining in JavaScript. Typically, only 1-level prototype chains are used to keep things simple, and because multi-level prototype chains are hard to get right. This library makes it really easy to use any number of levels in the prototype chain, which helps keep code DRY.
inherit( childConstructor, parentConstructor )
function Grandfather() {}
Grandfather.prototype.getName = function () { return 'John Doe I'; };
function Father() {}
Father.prototype.getName = function () { return 'John Doe II'; };
inherit( Father, Grandfather );
function Child() {}
Child.prototype.getName = function () { return 'John Doe III'; };
inherit( Child, Father );
var john_iii = new Child();
ECMAScript 3rd Edition and above. The script uses a polyfill for providing Object.create
in older browsers.
Note: Since property descriptors cannot be set in versions before ECMAScript 5, the constructor
property will be enumerable after a child has been extended.