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
| 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) { var checkArgs = argChecker(key, args, argTypes[key]); return Reflect.apply(value, target, args); }; }, });
function argChecker(name, args, checkers) { 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', {}); obj.pickyMethodOne({}, 'a little string', 123); obj.pickyMethodOne(123, {});
|