Skip to content

Instantly share code, notes, and snippets.



Last active Aug 29, 2015
What would you like to do?
// JavaScript inheritance with comments
function extends(Derived, Base) {
// First, we copy all static members from base to derived.
for (var staticMember in Base)
if (Base.hasOwnProperty(staticMember))
Derived[staticMember] = Base[staticMember];
// Then, we create a tiny helper "class" that constructs derived's prototype,
// which just adds the constructor property to its instance.
DerivedProtoMaker.prototype = Base.prototype;
function DerivedProtoMaker() { this.constructor = Derived; }
// and finally we make the derived classes prototype.
Derived.prototype = new DerivedProtoMaker();
// Alternative for the last 3 lines
// Derived.prototype.__proto__ = Base.prototype
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment