-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathunderclass.js
146 lines (128 loc) · 4.45 KB
/
underclass.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
(function(_) {
"use strict";
var keywords = {
'autoProperty' : /^\$(\w+)$/,
'autoObjProperty' : /^\$\$(\w+)$/,
'privateProperty' : /^__(\w+)__$/,
'super' : /^_super$/,
'self' : /^self$/,
'constructor' : /^constructor$/,
'extends' : /^extends$/
};
// annotate function borrowed from angularjs
// (returns argument names)
var FN_ARGS = /^function\s*[^\(]*\(\s*([^\)]*)\)/m;
var FN_ARG_SPLIT = /,/;
var FN_ARG = /^\s*(_?)(.+?)\1\s*$/;
var STRIP_COMMENTS = /((\/\/.*$)|(\/\*[\s\S]*?\*\/))/mg;
function annotate(fn) {
var $inject,
fnText,
argDecl,
last;
if (typeof fn === 'function') {
if (!($inject = fn.$inject)) {
$inject = [];
fnText = fn.toString().replace(STRIP_COMMENTS, '');
argDecl = fnText.match(FN_ARGS);
_.each(argDecl[1].split(FN_ARG_SPLIT), function(arg) {
arg.replace(FN_ARG, function(all, underscore, name) {
$inject.push(name);
});
});
}
}
return $inject;
}
_.mixin({
class : function() {
var _parent;
var definition;
if( _(arguments[0]).isFunction() ){
_parent = arguments[0];
definition = arguments[1];
} else {
definition = arguments[0];
}
//class constructor
function _class() {
if( _.isUndefined( definition.initialize)){
return;
}
var autoProperties = annotate(definition.initialize);
var _arguments = arguments;
var that = this;
// self and super are not properties
autoProperties = _.reject(autoProperties, function(property){
return property.match(keywords.self) || property.match(keywords.super);
});
_.each(autoProperties, function(property, index) {
var match;
match = property.match(keywords.autoProperty);
if ( match ) {
that[match[1]] = _arguments[index];
}
match = property.match(keywords.autoObjProperty);
if ( match && _.isObject(_arguments[index])) {
_.extend(that, _arguments[index]);
}
});
this.initialize.apply(this, arguments);
};
// Set up prototype chain
_class.prototype.constructor = _class;
function _subclass() {};
if ( _parent ) {
_subclass.prototype = _parent.prototype;
_class.prototype = new _subclass;
}
var methodNames = _.chain(definition)
.keys()
.filter( function(method){
return _.isFunction(definition[method]);
})
.value();
// Wrap methods when there is an argument called self or base ( or both of them )
// injecting the specified values.
_.each(methodNames, function(method) {
var argNames = annotate(definition[method]);
if( argNames.length === 0 ){
//no arguments, don't wrap method
_class.prototype[method] = definition[method];
} else {
//TODO: refactor this shit
if( argNames.length >= 2 && argNames[0].match(keywords.self) && argNames[1].match(keywords.super) ){
// "self", "super" as first and second argument
_class.prototype[method] = function(){
var args = _.toArray(arguments);
var _super = _parent.prototype[method].bind(this);
args.unshift(this, _super);
return _class.prototype[method].fn.apply(this, args);
};
} else if( argNames[0].match(keywords.self) ) {
// "self" as first argument
_class.prototype[method] = function(){
var args = _.toArray(arguments);
args.unshift(this);
return _class.prototype[method].fn.apply(this, args);
};
} else if( argNames[0].match(keywords.super) ) {
// "super" as first argument
_class.prototype[method] = function(){
var args = _.toArray(arguments);
var _super = _parent.prototype[method].bind(this);
args.unshift(_super);
return _class.prototype[method].fn.apply(this, args);
};
} else {
// no keywords matched, don't wrap function
_class.prototype[method] = definition[method];
}
}
//store the original unwrapped method
_class.prototype[method].fn = definition[method];
});
return _class;
}
});
})(_);