-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoojs.js
55 lines (48 loc) · 1.61 KB
/
oojs.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
var Extend = "extends";
/**
* Returns a class definition
* @param {string} [fnName=Object] - Name for your class (this will output in console when debugging an instance of a class)
* @param {string} [operation] - Operation to perform {extend(s)}
* @param {class} [related] - Related class; for operation: extend, this is the parent
* @param {function} - The class constructor
*/
var Class = function( fnName, operation, related, func ) {
var a = arguments;
var name = a[0],
operation = a[1],
parent = a[2],
fn = a[a.length - 1], // last argument
isExtend,
ret; // the return
// optional name not given //
if( typeof name != "string" ) {
name = "Object";
operation = a[0];
parent = a[1];
}
isExtend = ( typeof operation == "string" && (operation.toLowerCase() == "extends" || operation.toLowerCase() == "extend") ) ? true : false;
var createClass = function(c, ext, isExtend) {
var cls = new Function('c', 'ext', 'isExtend', // http://stackoverflow.com/a/9947842
"return function " + fnName + "(){" +
"if( isExtend )" +
"ext.apply(this, arguments);" +
"return c.apply(this, arguments);" +
"}"
)(c, ext, isExtend);
if( isExtend )
cls.prototype = Object.create( ext.prototype );
cls.prototype.constructor = cls;
return cls;
};
ret = createClass(fn, parent, isExtend);
if( isExtend ) {
ret.prototype._super = function() {
var s = new parent();
for( var i in s )
if( typeof s[i] == 'function' )
s[i] = s[i].bind(this);
return s;
};
}
return ret;
};