Skip to content

Instantly share code, notes, and snippets.

@spion
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save spion/9822395 to your computer and use it in GitHub Desktop.
Save spion/9822395 to your computer and use it in GitHub Desktop.
// 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