-
-
Save 3l3n01/5842885 to your computer and use it in GitHub Desktop.
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 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