Created
January 25, 2016 10:05
-
-
Save JustinTW/2d7e4b5af2de5cfaaba2 to your computer and use it in GitHub Desktop.
klass.js
This file contains hidden or 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
var klass = function (Parent, props) { | |
var Child, F, i; | |
// 1. | |
// new constructor | |
Child = function () { | |
if (Child.uber && Child.uber.hasOwnProperty("__construct")) { | |
Child.uber.__construct.apply(this, arguments); | |
} | |
if (Child.prototype.hasOwnProperty("__construct")) { | |
Child.prototype.__construct.apply(this, arguments); | |
} | |
}; | |
// 2. | |
// inherit | |
Parent = Parent || Object; | |
F = function () {}; | |
F.prototype = Parent.prototype; | |
Child.prototype = new F(); | |
Child.uber = Parent.prototype; | |
Child.prototype.constructor = Child; | |
// 3. | |
// add implementation methods | |
for (i in props) { | |
if (props.hasOwnProperty(i)) { | |
Child.prototype[i] = props[i]; | |
} | |
} | |
// return the "class" | |
return Child; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment