-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path方法检测.js
33 lines (32 loc) · 1.01 KB
/
方法检测.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
let obj = {
pickyMethodOne: function (obj, str, num) { /* ... */ },
pickyMethodTwo: function (num, obj) { /*... */ }
};
const argTypes = {
pickyMethodOne: ["object", "string", "number"],
pickyMethodTwo: ["number", "object"]
};
obj = new Proxy(obj, {
get: function (target, key, proxy) {
var value = target[key];
return function (...args) {
//console.log(args)
var checkArgs = argChecker(key, args, argTypes[key]);
return Reflect.apply(value, target, args);
};
}
});
function argChecker(name, args, checkers) {
if (args.length < checkers.length) {
console.warn('parameters count incorrect.')
}
for (var idx = 0; idx < args.length; idx++) {
var arg = args[idx];
var type = checkers[idx];
if (!arg || typeof arg !== type) {
console.warn(`You are incorrectly implementing the signature of ${name}. Check param ${idx + 1}`);
}
}
}
obj.pickyMethodOne();
obj.pickyMethodTwo("wopdopadoo", {});