Created
October 27, 2012 02:54
-
-
Save bloodyowl/3962770 to your computer and use it in GitHub Desktop.
dead simple Class function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] | |
} | |
}) | |
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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