rs.class.js
3.4 KB
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
/**
* Class类定义
*/
define(function () {
return function () {
var superClass = null;
var options = {};
var prototype = {};
for (var i = 0; i < arguments.length; i++) {
if (typeof arguments[i] === "function") {
superClass = arguments[i].prototype;
} else if (typeof arguments[i] === "object") {
options = arguments[i];
}
}
if (superClass) {
prototype = superClass;
for (var m in options) {
if (prototype.hasOwnProperty(m)) {
if (prototype[m] instanceof Array) {
prototype[m].push(options[m]);
} else {
var array = [prototype[m]];
array.push(options[m]);
prototype[m] = array;
}
} else {
prototype[m] = options[m];
}
}
} else {
for (var m in options) {
prototype[m] = options[m];
}
}
var currentClass = function () {
this.invokeMethod("variable", arguments);
this.invokeMethod("initialize", arguments);
this.invokeMethod("handler", arguments);
};
currentClass.prototype = prototype;
currentClass.prototype.invokeMethod = function (name, args) {
var methods = this.getProperty(name);
if (methods) {
if (methods instanceof Array) {
for (var i = 0; i < methods.length; i++) {
execMethod(this, methods[i]);
}
} else {
execMethod(this, methods);
}
}
function execMethod(inst, method) {
if (typeof method === "function") {
method.apply(inst, args);
} else {
this.log(name + "is not function");
}
}
};
/**
* 添加属性
* @param name
* @param prop
*/
currentClass.prototype.setProperty = function (name, prop) {
var tempProp = this.getProperty(name);
tempProp = prop;
};
/**
* 追加属性
* @param name
* @param prop
*/
currentClass.prototype.appendProperty = function (name, prop) {
var tempProp = this.getProperty(name);
if (tempProp) {
if (methods instanceof Array) {
tempProp.push(prop);
} else {
tempProp = prop;
}
}
};
/**
* 按名称得到当前属性
* @param name
* @returns {*}
*/
currentClass.prototype.getProperty = function (name) {
var tempProp;
for (prop in this) {
if (prop == name) {
tempProp = this[prop];
break;
}
}
return tempProp;
};
/**
* 向控制台输出信息
* @param info
*/
currentClass.prototype.log = function (info) {
if (info && console && console.log) {
console.log(info);
}
};
return currentClass;
}
});