Skip to content

Instantly share code, notes, and snippets.

@fat
Created March 9, 2011 06:33
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 fat/861782 to your computer and use it in GitHub Desktop.
Save fat/861782 to your computer and use it in GitHub Desktop.
/* this shit is the dumb... ignore pls */
function clone(obj){
if(obj == null || typeof(obj) != 'object')
return obj;
var temp = {};
for(var key in obj) temp[key] = clone(obj[key]);
return temp;
}
function append(original){
for (var i = 1, l = arguments.length; i < l; i++){
var extended = arguments[i] || {};
for (var key in extended)original[key] = extended[key];
}
return original;
}
/* this shit is the luke-warm... */
!function (context) {
function methods(o) {
for (var k in o) {
o.hasOwnProperty(k) && wrapper.call(this, o, k);
}
return this;
}
function statics(o) {
for (var k in o) {
o.hasOwnProperty(k) && (this[k] = o[k]);
}
return this;
}
function setOptions(o) {
var options = this.options = append(clone(this.options), o);
return this;
}
function options(o) {
this.prototype.options = {};
for (var k in o) {
o.hasOwnProperty(k) && (this.prototype.options[k] = o[k]);
}
return this;
}
function wrapper (o, k) {
var sup = this.prototype.constructor.sup;
this.prototype[k] = function () {
this._name = k;
this.sup = sup;
return o[k].apply(this, arguments);
};
}
function klass(fn) {
var constructor = function () {
debugger;
if (this.options) {
setOptions.call(this, arguments[arguments.length - 1]); //<-- is dumb tho. :{o
}
return fn.apply(this, arguments);
}
constructor.methods = methods;
constructor.statics = statics;
constructor.options = options;
return constructor;
}
function extend(sup, sub) {
function fn() {
sup.apply(this, arguments);
sub.apply(this, arguments);
}
var F = function(){};
F.prototype = sup.prototype;
fn.prototype = new F();
fn.methods = methods;
fn.statics = statics;
fn.prototype.constructor = sub;
fn.prototype.constructor.sup = sup;
fn.prototype.supr = function () {
if (this.sup.prototype[this._name]) {
return this.sup.prototype[this._name].apply(this, arguments);
}
};
return fn;
}
if (typeof module !== 'undefined' && module.exports) {
module.exports = {
klass: klass,
extend: extend
};
} else {
context.klass = klass;
context.extend = extend;
}
}(this);
/* usage.. the hows: */
var Person = klass(function (name) {
this.name = name;
console.log(this.options.hair); //<-- should be false
})
.options({
hair: true
})
.statics({
head: ':)',
feet: '_|_'
})
.methods({
walk: function () {}
});
new Person('dick', {
hair: false
});
@assertchris
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment