-
Notifications
You must be signed in to change notification settings - Fork 0
/
13.js
97 lines (95 loc) · 3.49 KB
/
13.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
class Type {
static typeEnum = ['undefined', 'null', 'boolean', 'number', 'string', 'function', 'array', 'date', 'regExp', 'object', 'error']
get class2type() {
return {
'[object Undefined]': 'undefined',
'[object Null]': 'null',
'[object Boolean]': 'boolean',
'[object Number]': 'number',
'[object String]': 'string',
'[object Function]': 'function',
'[object Array]': 'array',
'[object Date]': 'date',
'[object RegExp]': 'regExp',
'[object Object]': 'object',
'[object Error]': 'error'
}
}
static getType(obj) {
var classname = Object.prototype.toString.call(obj)
return this.#class2type[classname] ?? this.typeEnum.object
}
}
class Methods {
#methods = {}
#compareArray(arr1, arr2) {
if (Array.isArray(arr1) && Array.isArray(arr2)) {
return (arr1.length == arr2.length) && arr1.every((o, i) => o === arr2[i])
} else {
throw new TypeError(`Failed to compare array: Invalid Array`)
}
}
register(name, fn, ...argTypes) {
if (Type.getType(fn) != 'function') {
throw new TypeError(`Failed to register '${name}': Invalid function`)
}
if (fn.length > argTypes.length) {
throw new TypeError(`Failed to register '${name}': at least ${fn.length} argument required, but only ${argTypes.length} present.`);
}
if (Array.isArray(this.#methods[name])) {
for (let i in this.#methods) {
if (this.#compareArray(this.#methods[name][i].argTypes, argTypes)) {
// 去除参数相同项
this.#methods[name].splice(i, 1);
break;
}
}
this.#methods[name].push({ fn, argTypes });
} else {
this.#methods[name] = [{ fn, argTypes }];
}
}
invoke(name, ...args) {
if (Array.isArray(this.#methods[name])) {
for (let i in this.#methods[name]) {
if (this.#compareArray(this.#methods[name][i].argTypes, args.map(a => Type.getType(a)))) {
return this.#methods[name][i].fn(...args);
}
}
throw new ReferenceError(`Failed to invoke ${name}: arguments miss match: [${args.map(a => Type.getType(a))}].`);
} else {
throw new ReferenceError(`Failed to invoke ${name}: ${name} is not defined`);
}
}
delete(name, ...argTypes) {
if (Array.isArray(this.#methods[name])) {
for (let i in this.#methods[name]) {
if (this.#compareArray(this.#methods[name][i].argTypes, argTypes)) {
if (this.#methods[name].length == 1) {
delete this.#methods[name];
} else {
this.#methods[name].splice(i, 1);
}
return;
}
}
throw new ReferenceError(`Failed to delete ${name}: arguments miss match: [${argTypes}].`);
} else {
console.warn(`Failed to delete ${name}: ${name} is not defined`);
}
}
}
class UrlBase {
#methods = new Methods()
#init() {
this.#methods.register('constructor', () => {
console.log('测试')
})
}
constructor() {
this.#init();
this.#methods.invoke('constructor', ...arguments);
}
toString() {
}
}