简体   繁体   English

如何将变量传递给全局范围

[英]how to pass variable to global scope

Question - how to pass variable to global scope? 问题 - 如何将变量传递到全局范围? :

var global_variable; var global_variable;

chrome.storage.local.get('ABC',function(result){
            global_variable = result; //pass result to global var.
});

console.log(global_variable); //outputs undefined, why!!!?

It's just because console.log() is launched before the 'ABC',function(result){ set the global var. 这只是因为console.log()'ABC',function(result){之前启动'ABC',function(result){设置全局'ABC',function(result){
Because chrome.storage.local.get is launched an asynchronously. 因为chrome.storage.local.get是异步启动的。

To verifiy it, test this code : 要验证它,请测试以下代码:

var global_variable;
chrome.storage.local.get('ABC',function(result){
        global_variable = result; //pass result to global var.
        test();
});
function test() {
    console.log(global_variable);
}

You're setting the variable inside an async function callback. 您正在异步函数回调中设置变量。 The callback runs after the get action has completed. get动作完成后运行回调。

Your code will execute in this order: 您的代码将按此顺序执行:

chrome.storage.local.get("ABC",callback);

then 然后

console.log();

then 然后

callback()

You can verify that by putting a console.log statement in your callback and seeing that it runs after the current one. 您可以通过在回调中放置console.log语句并查看它在当前回调之后运行来验证这一点。

Javascript functions run to completion before they run any asynchronous callback calls. Javascript函数在运行任何异步回调调用之前运行完成。

It may make more sense written like this, which is equivalent to what you have right now. 这样写得更有意义,这相当于你现在所拥有的。

//define the callback
function callbackFunction(result){
      global_variable = result; //pass result to global var.
}

//run the get.  When it is complete the callback will be added to a queue 
//to run next after current operations have completed
chrome.storage.local.get('ABC',callback);

// this will run immediately after the get call
console.log(global_variable);

If you want to work with your variable you will have to put the logic in the callback (or in a separate function called by the callback. 如果要使用变量,则必须将逻辑放在回调中(或者在回调调用的单独函数中)。

I'm assuming, not knowing what the returned value of 'result' is, that you wish to pass a value to global scope to a variable that has not been predefined. 我假设,不知道“结果”的返回值是什么,您希望将值传递给全局范围到尚未预定义的变量。 The following callback change will both define a new variable with global scope, and assign the value of 'results' to it. 以下回调更改将定义具有全局范围的新变量,并为其分配“结果”的值。 Remember, that scope has to do with hierarchy within the window object, and not the script. 请记住,该范围与窗口对象中的层次结构有关,而与脚本无关。

function callbackFunction(result){
    window.global_variable = result;
}
console.log(global_variable);

You could also dynamically inject a script tag, with the src attribute being your global value. 您还可以动态注入脚本标记,src属性是您的全局值。

function callbackFunction(result){
    var js = document.createElement('script');
    js.src = 'var global_variable = ' + result + ';';
    var first = document.getElementsByTagName('script')[0];
    first.parentNode.insertBefore(js, first);
}

Although I think this seems a bit verbose for achieving the same results. 虽然我认为这对于实现相同的结果似乎有点冗长。

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

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