gpt4 book ai didi

javascript - 在 Node.js 中记录每个函数调用,而不进行无限递归

转载 作者:太空宇宙 更新时间:2023-11-03 22:37:43 27 4
gpt4 key购买 nike

我创建的函数必须记录每个函数调用。

(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;
})();

但它无法 stackoverflow:

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;
}

但不知 Prop 体该怎么做。

最佳答案

您可以在应用调用之前添加:

 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:未定义不是函数”。检查这个、self 和参数是否未定义没有帮助:-(。

关于javascript - 在 Node.js 中记录每个函数调用,而不进行无限递归,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22978706/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com