Skip to content

Instantly share code, notes, and snippets.

@kaheglar
Created January 5, 2012 11:19
Show Gist options
  • Save kaheglar/1564802 to your computer and use it in GitHub Desktop.
Save kaheglar/1564802 to your computer and use it in GitHub Desktop.
Simple JavaSctipt Inheritance Module - ASM Compliant
/**
* Class Module (No Dependencies)
*/
define(function() {
// Set to true when creating New Prototype to ensure "_constructor" is not executed
var extending = false;
// Base Constructor
var Class = function() {};
// Create Subclass
Class.extend = function(properties) {
// New Constructor
function Class() {
// Delegate to _constructor function
if(!extending && this._constructor) {
this._constructor.apply(this, arguments);
}
};
// New Prototype
extending = true;
var prototype = new this();
extending = false;
// Set Constructor
prototype.constructor = Class;
// Set Super
prototype._super = this.prototype;
// Set Prototype
Class.prototype = prototype;
// Set Class Functions
Class.mixin = this.mixin;
Class.extend = this.extend;
// Add Properties
Class.mixin(properties);
return Class;
};
// Extend Class Prototype
Class.mixin = function(properties) {
extend(this.prototype, properties);
};
// Extend Instance
Class.prototype.mixin = function(properties) {
extend(this, properties);
};
// Extend Utility (to ensure no external dependencies)
function extend(dest, src) {
if (src) {
for (var prop in src) {
if (src.hasOwnProperty(prop)) {
dest[prop] = src[prop]
}
}
}
return dest
}
// Export "Class" Constructor
return Class;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment