Skip to content

Instantly share code, notes, and snippets.

@AlexanderBrevigSublime
Created October 1, 2012 20:35
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 AlexanderBrevigSublime/3814257 to your computer and use it in GitHub Desktop.
Save AlexanderBrevigSublime/3814257 to your computer and use it in GitHub Desktop.
JavaScript: Class simulation
/// 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