简体   繁体   English

Javascript从嵌套函数中调用外部函数

[英]Javascript calling outer function from within nested function

Having what I thought should be a relatively easy problem to deal with being a major pain... I am trying to do: 拥有我认为应该是一个相对容易的问题来处理一个主要的痛苦...我想做:

a.b("param", function(data)
{
     logger.debug("a.b(" + data.toString() + ")");

     if (data.err == 0)
     {
           // No error, do stuff with data
     }
     else
     {
           // Error :(  Redo the entire thing.
     }
});

My approach to this was to try: 我的方法是尝试:

var theWholeThing = function() {return a.b("param", function(data)
{
     logger.debug("a.b(" + data.toString() + ")");

     if (data.err == 0)
     {
           // No error, do stuff with data
     }
     else
     {
           // Error :(  Redo the entire thing.
           theWholeThing();
     }
})};

The problem with the above is while the former did work (except didnt deal when an error occurred), the latter simply never prints the log message... its as if "theWholeThing()" call isn't working as I thought it should (call the whole thing again). 上面的问题是前者确实有效(除非在发生错误时没有处理),后者根本就不打印日志消息......就好像“theWholeThing()”调用不起作用,因为我认为应该(再次调用整个事情)。

There must be something subtly wrong here, any tips? 这里肯定有一些巧妙的错误,任何提示?

Thanks! 谢谢!

First, to answer you question directly, it sounds like you forgot to actually call the function the first time. 首先,直接回答你的问题,听起来你忘了第一次实际调用这个函数。 Try: 尝试:

var theWholeThing = function() {return a.b("param", function(data)
{
     logger.debug("a.b(" + data.toString() + ")");

     if (data.err == 0)
     {
           // No error, do stuff with data
     }
     else
     {
           // Error :(  Redo the entire thing.
           theWholeThing();
     }
})};

theWholeThing(); // <--- Get it started.

However, this can be achieved a little more elegantly in a named IIFE (immediately invoked function expression): 但是,这可以在命名的IIFE(立即调用的函数表达式)中更优雅地实现:

// We wrap it in parentheses to make it a function expression rather than
// a function declaration.
(function theWholeThing() {
    a.b("param", function(data)
    {
         logger.debug("a.b(" + data.toString() + ")");

         if (data.err == 0)
         {
               // No error, do stuff with data
         }
         else
         {
               // Error :(  Redo the entire thing.
               theWholeThing();
         }
    });
})(); // <--- Invoke this function immediately.

If you separate the methods apart and use variables to represent, things become clear. 如果将方法分开并使用变量来表示,事情就会变得清晰。 You just need to treat your ab and anonymous function as method reference. 您只需要将ab和匿名函数视为方法参考。 I think this code sample can help: 我认为此代码示例可以提供帮助:

var theWholeThing = a.b, //this is a reference of b method
    par = "param",
    callback = function(data){
     logger.debug("a.b(" + data.toString() + ")");

     if (data.err == 0)
     {
           console.log("no error");    
     }
     else
     {
           console.log("Error");
           // theWholeThing reference can be accessed here and you can pass your parameters along with the callback function using variable name 
           theWholeThing("param", callback); 
     }
}; //declare the callback function and store it in a variable

theWholeThing("param", callback); //invoke it

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

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