4
簡単な再帰関数を想像してみましょう。これは入力と出力を計測するためにラップしようとしています。再帰関数のプロキシ
// A simple recursive function.
const count = n => n && 1 + count(n-1);
// Wrap a function in a proxy to instrument input and output.
function instrument(fn) {
return new Proxy(fn, {
apply(target, thisArg, argumentsList) {
console.log("inputs", ...argumentsList);
const result = target(...argumentsList);
console.log("output", result);
return result;
}
});
}
// Call the instrumented function.
instrument(count)(2);
しかし、これは最上位レベルで入力と出力を記録します。私はcount
を再帰的に呼び出す際に計測バージョンを呼び出す方法を見つけたいと思います。
私はあなたが 'count'をオーバーライドしなければならないと信じています。 JavaScriptにはレキシカルスコープがあるので、関数内で 'count'を何か他のものを参照するために行うことはできません(' count'をオーバーライドすることを除く)。しかし、私は間違っていることが証明されてうれしいです:) –