简体   繁体   English

从javascript对象方法回调中检索变量

[英]Retrieve variable from javascript object method callback

In my code I need to call an object method, retrieve the data from its callback, and pass it to another method or function. 在我的代码中,我需要调用一个对象方法,从其回调中检索数据,并将其传递给另一个方法或函数。

someObject.getSomeData({option1:'value1', option2:'value2'},
    function(data) {
        doAwesomeStuff(data);
    }
);

However, the callback does not recognize any functions/objects/variables outside its scope. 但是,回调无法识别超出其范围的任何函数/对象/变量。

What I've tried to do right now is wrap everything around a function. 我现在尝试做的是将所有内容包装在一个函数中。

var myData = '';
(function(myData) {
    someObject.getSomeData({option1:'value1', option2:'value2'},
        function(data) {
            myData = data;
        }
    );
});
doAwesomeStuff(myData);

However that doesn't work either. 但是,这也不起作用。

Anybody know how to properly accomplish this? 有人知道如何正确地做到这一点吗?

You haven't really given us enough to go on there, but this statement: 您尚未真正给我们足够的支持,但是请执行以下声明:

However, the callback does not recognize any functions/objects/variables outside its scope. 但是,回调无法识别超出其范围的任何函数/对象/变量。

...is incorrect. ...是不正确的。 A function has access to everything in scope where it's defined, so for instance: 函数可以访问定义它的作用域中的所有内容,例如:

var a = 10;
function foo(b) {
    bar(5);
    function bar(c) {
        alert(a + b + c);
    }
}
foo(12); // alerts "27"

Note how bar had access not only to c , but also to b (from the call to foo ) and a (from the outermost scope shown). 请注意, bar如何不仅访问c ,而且还访问b (从对foo的调用)和a (从所示的最外部作用域)访问。

So in your example, the anonymous function you're passing in as the callback has access to everything that's in scope where it's defined; 因此,在您的示例中,作为回调传递的匿名函数可以访问定义范围内的所有内容; doAwesomeStuff having been defined elsewhere presumably has access to different information, so you'll have to have the callback pass it any data it needs. 在其他地方定义的doAwesomeStuff大概可以访问不同的信息,因此您必须使回调将需要的任何数据传递给它。

So I'm guessing your code looks something like this: 所以我猜你的代码看起来像这样:

function doAwesomeStuff(data) {
    // ...
}

function doSomethingNifty() {
    var a = 10,
        b = 20;
    someObject.getSomeData({option1:'value1', option2:'value2'},
        function(data) {
            doAwesomeStuff(data);
        }
    );
}

...and you want doAwesomeStuff to have access to a and b from the call to doSomethingNifty . ...并且您希望doAwesomeStuff可以从对doSomethingNifty的调用中访问ab If so, your only options are to pass them into it as arguments (probably best) or export them to variables some scope that doSomethingNifty and doAwesomeStuff share (probably not ideal, too much like globals). 如果是这样,您唯一的选择是将它们作为参数传递给它(可能是最好的)或将它们导出到doSomethingNiftydoAwesomeStuff共享的某个范围的变量(可能不理想,与全局变量很像)。

You can bind required variables to the function passed into the async method. 您可以必需的变量绑定到传递给async方法的函数。

Also, this SO question has a good treatment of the topic. 同样, 此SO问题对该主题也有很好的处理。

Your second version is not going to work at all, since you are trying to immediately access the data that are not yet available (not until the callback has been invoked.) 您的第二个版本根本无法使用,因为您试图立即访问尚不可用的数据(直到调用了回调之后)。

Your first method: 您的第一种方法:

someObject.getSomeData({option1:'value1', option2:'value2'},
    function(data) {
        doAwesomeStuff(data);
    }
);

looks fine. 看起来不错。 Please provide more details on what is not working. 请提供更多有关不起作用的详细信息。

One problem could be that getSomeData() does not actually call the callback function. 一个问题可能是getSomeData()实际上没有调用回调函数。

doAwesomeStuff() can modify many different variables from the received data. doAwesomeStuff()可以从接收到的数据中修改许多不同的变量。 The variables which can be accessed by doAwesomeStuff() are those that were available to it (in its scope ) where it was created.. doAwesomeStuff()可以访问的变量是创建它的地方(在其范围内 )可用的那些。

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

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