Created
August 9, 2012 18:01
-
-
Save robdodson/3306573 to your computer and use it in GitHub Desktop.
klass
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
| (function() { | |
| var klass = function(Parent, props) { | |
| var Child, F, i; | |
| // 1. | |
| // new constructor | |
| Child = function() { | |
| // Call the super class constructor | |
| if (Child._super && Child._super.hasOwnProperty('_construct')) { | |
| Child._super._construct.apply(this, arguments); | |
| } | |
| // Call our constructor | |
| 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._super = Parent.prototype; | |
| Child.prototype.constructor = Child; | |
| // 3. | |
| // add implementation methods | |
| for (i in props) { | |
| if (props.hasOwnProperty(i)) { | |
| Child.prototype[i] = props[i]; | |
| } | |
| } | |
| // 4. | |
| // return the "class" | |
| return Child; | |
| } | |
| })(); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment