admin管理员组文章数量:1425896
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).
There must be something subtly wrong here, any tips?
Thanks!
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).
There must be something subtly wrong here, any tips?
Thanks!
Share Improve this question asked Dec 3, 2012 at 5:31 Jordan JohnsJordan Johns 7452 gold badges10 silver badges20 bronze badges 02 Answers
Reset to default 5First, 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):
// 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 bee clear. You just need to treat your a.b and anonymous function as method reference. 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
本文标签: Javascript calling outer function from within nested functionStack Overflow
版权声明:本文标题:Javascript calling outer function from within nested function - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745380551a2656139.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论