Skip to content

Instantly share code, notes, and snippets.

@ded
Created October 15, 2010 18:13
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 ded/628666 to your computer and use it in GitHub Desktop.
Save ded/628666 to your computer and use it in GitHub Desktop.
A simple Class system with a familiar interface
(function() {
window.twttr = window.twttr || {};
aug(twttr, {
klass: function(ns, init) {
var c = function() {
init.apply(this, arguments);
};
augString(ns, c);
c.name = ns.split(".").pop().toLowerCase();
c.methods = twttr.methods;
c.statics = twttr.statics;
c._namespace = ns;
c._isKlass = true;
c.superclass = function(superc) {
if (!superc._isKlass) {
throw ('You cant something that is not a twttr.klass');
}
twttr.extend(c, superc);
c.uberclass = superc;
return c;
};
return c;
},
extend: function(subc, superc) {
var F = function() {};
F.prototype = superc.prototype;
subc.prototype = new F();
subc.prototype.constructor = subc;
subc.uber = superc.prototype;
if (superc.prototype.constructor == Object.prototype.constructor) {
superc.prototype.constructor = superc;
}
},
methods: function(o) {
this.prototype._namespace = this._namespace;
for (var methodName in o) {
this.prototype[methodName] = o[methodName];
this.prototype[methodName]._methodName = methodName;
}
return this;
},
statics: function(o) {
return aug(this, o);
}
});
function aug(ns, o) {
for (var key in o) {
ns[key] = o[key];
}
return ns;
}
function augString(str, value, replace) {
var o = window;
var s = str.split('.');
for (var i = 0, len = s.length; i < len; ++i) {
o = o[s[i]] = o[s[i]] || (s[i + 1]) ? {} : value);
}
return o;
}
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment