-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathjquery.classbuilder.js
47 lines (37 loc) · 1.43 KB
/
jquery.classbuilder.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
(function($){
$.buildClass = function(){
var argc = 0;
//Methods of the current class
var methods = arguments[argc]?arguments[argc++]:{};
//Definition of the upper class or empty class if no one is provided
var P = $.isFunction(arguments[argc])?arguments[argc]:function(){};
//Class of the object uber
var Uber = function(base){
if(P.prototype.__U) this.uber = new P.prototype.__U(base);
this.__o = base;
}
//For each function defined in the parent class, we have to dynamically generate one for the uber object throw eval
for(var f in P.prototype){
if($.isFunction(P.prototype[f])){
//Use eval to generate all functions « on the fly ». Otherwize you're screwed up.
Uber.prototype[f] = eval('(function(){return function(){return P.prototype[\''
+ f.replace(/\\'/g, '\\\'').replace(/\\/g, '\\')
+ '\'].apply(this.__o, arguments);};})();' );
}
}
var Constructor = function(){
//Set the uber object
this.uber = new Uber(this);
//Run the provided contructor if it exists
if(methods.constructor) methods.constructor.apply(this, arguments);
}
//create a new prototype for our class
var F = function(){}
F.prototype = P.prototype;
Constructor.prototype = new F();
//Set assign methods to this prototype
$.extend(Constructor.prototype, methods, { __U : Uber });
Constructor.prototype.constructor = Constructor;
return Constructor;
}
})(jQuery);