我创建了一个函数包装器来帮助我提供各种打印语句:
function fnWrapper(fn, verbosity=false) {
return function(...rest) {
if (verbosity) console.log(`Calling "${fn.name}(${rest})"`)
const res = fn(...rest); // possible to add debugging **within** this wrapped function?
if (verbosity) console.log('==>', res)
return res;
}
}
function add(x,y) {
return x===0 ? y : add(x-1, y+1);
}
const add2 = fnWrapper(add, true);
add2(2,3);
// Calling "add(2,3)"
// ==> 5
是否可以在函数本身中添加调试,例如,在最基本的情况下转换函数,例如:
function something(x,y) {
console.log(arguments); // add this in
...
}
因此,对于上述功能,它将使add
变为:
function add(x,y) {
console.log(arguments);
return x===0 ? y : add(x-1, y+1);
}
如果是的话,那怎么办呢?