Skip to content

Instantly share code, notes, and snippets.

@jdeveloper
Created February 19, 2009 11:42
Show Gist options
  • Save jdeveloper/66878 to your computer and use it in GitHub Desktop.
Save jdeveloper/66878 to your computer and use it in GitHub Desktop.
//function to define a class
// actual version is a rewrite by anieto2k: http://www.anieto2k.com
//params:
// @current : Object with the methods and attributes of the class
// @previous : Class from witch we wante to extend (optional)
JotaClass = function(current,previous){
previous = typeof previous == 'undefined' ? {} : previous.prototype;
for(p in previous){
if(typeof current[p] == 'undefined') current[p] = previous[p];
else if(typeof previous[p] == 'function'){
current[p] = (function(tmp){
return function(){
this.parent = arguments.callee.parent;
return tmp.apply(this, arguments);
}
})(current[p]);
current[p].parent = previous[p];
}
}
var construct = function(){
if(this.init) this.init.apply(this,arguments);
}
construct.prototype = current;
construct.constructor = Class;
return construct;
}
//example of Class definition
Persona = JotaClass({
init: function(nombre){
this.nombre = nombre;
},
hola: function(){
alert(this.nombre);
}
});
//Example of inhiritance
PersonaExt = JotaaClass({
hola: function(){
alert('gonna call parent');
this._super.hola();
}
},Persona);
//instance and calling methods
p1 = new Persona('lolo');
p2 = new PersonaExt('lolo')
p1.hola();
p2.hola();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment