Javascript 在node.js中记录每个函数调用,无需无限递归
我创建的函数必须记录每个函数调用Javascript 在node.js中记录每个函数调用,无需无限递归,javascript,node.js,Javascript,Node.js,我创建的函数必须记录每个函数调用 (function () { var oldCall = Function.prototype.call; var newCall = function(self) { Function.prototype.call = oldCall; console.log('Function called:', this.name); Function.prototype.call = newCall; this.apply(sel
(function () {
var oldCall = Function.prototype.call;
var newCall = function(self) {
Function.prototype.call = oldCall;
console.log('Function called:', this.name);
Function.prototype.call = newCall;
this.apply(self, Array.prototype.slice.call(arguments, 1));
}
Function.prototype.call = newCall;
})();
但它没有做到:
Function called: slice
Function called: slice
Function called: slice
Function called: slice
Function called: slice
Function called: slice
Function called: slice
RangeError: Maximum call stack size exceeded
如何修改它使其工作?
我想问题是slice
也是函数,它调用无限递归。也许可以进行如下过滤:
if (functionName == 'slice')
{
return;
}
但无法确定具体操作方法。您可以在应用调用之前添加:
if (this.name == 'slice')
{
return;
}
要获得:
(function () {
var oldCall = Function.prototype.call;
var newCall = function(self) {
if (this.name == 'slice')
{
return;
}
Function.prototype.call = oldCall;
console.log('Function called:', this.name);
Function.prototype.call = newCall;
this.apply(self, Array.prototype.slice.call(arguments, 1));
}
Function.prototype.call = newCall;
})();
但这并不是一个完整的解决方案,因为在我的代码中,我一直停留在“TypeError:undefined不是一个函数”上。
检查此项、self和参数是否未定义没有帮助:-(.产生错误的函数(不是原始的,而是通过使用此解决方案记录函数调用)是筛选函数,现在是来自路径模块的连接函数,它还使用了Array.prototype.filter.call,它现在是未定义的X-)。所以这不是一个好的解决方案对不起。。。