Skip to content

Instantly share code, notes, and snippets.

@bloodyowl
Created October 27, 2012 02:54
Show Gist options
  • Save bloodyowl/3962770 to your computer and use it in GitHub Desktop.
Save bloodyowl/3962770 to your computer and use it in GitHub Desktop.
dead simple Class function
function Class(o){
var klass = function(){ return o.init.apply(this, arguments) },
parent = o.parent,
prototype = "prototype",
i;
if(parent) {
klass[prototype] = parent[prototype];
klass[prototype].parent = function(){parent.apply(this, arguments)};
}
for(i in o) if(o.hasOwnProperty(i)) klass[prototype][i] = o[i];
return klass[prototype].constructor = klass;
}
/*
var Foo = new Class({
init: function (a) {
this.name = a
},
sayHi: function () {
return "hey" + this.name
},
setName: function (a) {
this.name = a;
return this
}
});
var Bar = new Class({
parent : Foo,
init : function(a, b){
this.parent(a);
this.age = b;
},
toArray : function(){
return [this.age, this.name]
}
})
*/
/* 235 bytes */
function Class(e){var t=function(){return e.init.apply(this,arguments)},n=e.parent,r="prototype",i;n&&(t[r]=n[r],t[r].parent=function(){n.apply(this,arguments)});for(i in e)e.hasOwnProperty(i)&&(t[r][i]=e[i]);return t[r].constructor=t}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment