不使用arguments.callee递归调用匿名函数

Abh*_*hek 3 javascript

如何递归地调用匿名函数(从其内部)?

(function(n) {
    console.log(n=n-1);
    // Want to call it recursively here without naming any function
})(20);
Run Code Online (Sandbox Code Playgroud)

bar*_*sny 5

只要说出它的名字就可以了。在这种情况下,该函数在括号之外将不可用 - 因此您不会用它污染命名空间。

(function recursive(n) {
  console.log(n = n - 1);
  if (n > 0) {
    recursive(n); // just an example
  }
})(20); 

//recursive(20); // won't be able to call it here...
Run Code Online (Sandbox Code Playgroud)