简体   繁体   English

如何在JavaScript中从内部调用匿名函数?

[英]How can I call an anonymous function from inside itself in JavaScript?

Is it possible to re-run the function 'check' without calling check() in the else statement? 是否可以重新运行功能'check'而无需在else语句中调用check()? Maybe call itself somehow? 也许以某种方式称呼自己?

If I have multiple instances of this function, they end up calling each other, unless I change it to check1, check2, etc.. While this is prob. 如果我有该函数的多个实例,除非我将其更改为check1,check2等,否则它们最终会互相调用。 a performance killer, I was just trying it out for a prototype app. 一个性能杀手,我只是在尝试一个原型应用程序。

(function check () {
  if (typeof foo !='undefined') {
    // do stuff
  } else {
    console.log("Failed to load, trying again");
    setTimeout(function(){ check(); }, 10);
  }
})();

Well, you could use arguments.callee to call it, but it's a bad idea, a performance hit, and it isn't valid in strict mode. 好了,您可以使用arguments.callee来调用它,但这不是一个好主意,会降低性能,并且在严格模式下无效。

What you've quoted is valid JavaScript and should not make check call another check function if you have one alongside it. 什么你引用的是有效的JavaScript, 不应该check调用另一个check功能,如果你有一个在它旁边。 Sadly, though, it will in IE8 and earlier because they quite incorrectly leak the name check to the surrounding scope (named function expressions do not add the function name to the surrounding scope like function declarations do — except on IE8 and earlier [and that's not all they get wrong with them]). 但是,令人遗憾的是, 它将在IE8和更早的版本中使用,因为它们很不正确地将名称check泄漏给了周围的范围(命名函数表达式没有像函数声明那样将函数名添加到周围的范围中,但在IE8和更早的版本上[并且不是他们都错了])。

So the solution is to use an anonymous function to hide a named one: 因此解决方案是使用匿名函数隐藏命名的函数:

(function() {
    // A check function
    check();
    function check () {
      if (typeof foo !='undefined') {
        // do stuff
      } else {
        console.log("Failed to load, trying again");
        setTimeout(function(){ check(); }, 10);
      }
    }
})();
(function() {
    // A complete different one
    check();
    function check () {
      if (typeof foo !='undefined') {
        // do other stuff
      } else {
        console.log("Failed to load, trying again");
        setTimeout(function(){ check(); }, 10);
      }
    }
})();

How about using an interval instead removing the need to call itself, as this will be done automatically until you clear the interval: 如何使用间隔而不是消除自身调用的需要,因为这将自动完成,直到您清除间隔:

(function(){
   var interval = setInterval(function(){
      if(typeof foo != 'undefined'){
         clearInterval(interval);
         // do stuff
      }else{
         console.log("Failed to load, trying again");
      }
   },10)
})();

Why not do something like this instead: 为什么不这样做呢?

var inter;

function check() {
    if (typeof foo !== 'undefined') {
        clearInterval(inter);
        // do stuff
    } else {
        console.log("Failed to load, trying again");
    }
}
inter = setInterval(check, 10);

It's not a drop in replacement, but I think it's better style. 这不是替换的下降,但我认为它是更好的样式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM