[英]Is there a way in JavaScript to reduce callstack size in recursive function calls?
[英]Is there an idiomatic way to prevent recursive function calls in JavaScript?
假设我有一些复杂的函数,通过随后的嵌套函数调用,可能会调用自身,是否有一种可接受的方法来防止这种递归调用在不受欢迎的情况下?
例如:
function h()
{
if(someOtherState)
f(); // uh-oh
}
function g()
{
if(someState)
h();
}
function f()
{
g();
}
在这里调用坎宁安定律(我不是真正的 JS 开发人员),我发现一种可行的方法是:
function f()
{
if(f.inFunction)
return;
f.inFunction = true;
// Some complicated/non-deterministic code that may call f() again
f.inFunction = false;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.