Last active
August 29, 2015 13:57
-
-
Save spion/9822395 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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