-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodule.js
61 lines (48 loc) · 1.16 KB
/
module.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
var slice = Array.prototype.slice;
function copy () {
var dest = arguments[0],
src = slice.call(arguments, 1);
for (var i = 0; i < src.length; i++) {
for (var key in src[i]) {
dest[key] = src[i][key];
}
}
}
function bind (fn, ctx) {
return function () {
return fn.apply(ctx, arguments);
}
}
function isFunction (obj) {
var checkType = {};
return obj && checkType.toString.call(obj) === '[object Function]';
}
function Module () {
this.bindMethods();
if (this.parent && this.parent.init && isFunction (this.parent.init)) {
this.parent.init.apply(this, arguments);
}
if (this.init && isFunction (this.init)) {
this.init.apply(this, arguments);
}
}
Module.extend = function (instance, static) {
var tmp = this;
var obj = function () {
return tmp.apply(this, arguments);
}
// Copy the instance methods
copy(obj.prototype, this.prototype, instance);
// Copy the static methods
copy(obj, this, static);
// Set up the "parent" object
obj.prototype.parent = this.prototype;
return obj;
}
Module.prototype.bindMethods = function () {
for (var key in this) {
if (isFunction (this[key])) {
this[key] = bind(this[key], this);
}
}
}