Created
October 1, 2012 20:35
-
-
Save AlexanderBrevigSublime/3814257 to your computer and use it in GitHub Desktop.
JavaScript: Class simulation
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
/// Make this script available somewhere | |
var extendsClass = this.extendsClass || function (d, b) { | |
function __inheritedProto() { this.constructor = d; } | |
__inheritedProto.prototype = b.prototype; | |
d.prototype = new __inheritedProto(); | |
} | |
/// Sample class 1 | |
var CLASS = (function() { | |
function CLASS(data) { | |
this.value = data; | |
} | |
CLASS.prototype.method = function() { | |
return this.value; | |
}; | |
return CLASS; | |
})(); | |
/// Sample class 2, extends class 1 | |
var CLASS2 = (function(_super) { | |
extendsClass(CLASS2, _super); | |
function CLASS2() { | |
_super.apply(this, arguments); | |
} | |
CLASS2.prototype.method2 = function() { | |
return this.value * 2; //do something else | |
}; | |
return CLASS2; | |
})(CLASS); | |
/// Create some instances | |
var instance = new CLASS(1); | |
var instance2 = new CLASS2(2); | |
/// Call some methods | |
instance.method(); // 1 | |
instance2.method(); // 2 | |
instance2.method2();// 4 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment