简体   繁体   中英

jQuery global variable access confusion

I have very specific problem, I am attempting to get the data using localforage library, assigning it to a global variable, which is a JSON.

$.unify.data.assets = {};
$.unify.initialization.private.intializeDefaultLayout = function() {
    localforage.getItem('assets', function($value) {
        $.unify.data.assets = $value;
        console.log($.unify.data.assets); // This shows all object in the record

    });
    console.log($.unify.data.assets); // This is empty, even on global variable assignment
};

I am very confused in this problem, kindly correct the required

Because this line is being executed in a callback

$.unify.data.assets = $value;

So there are chances the following line has been called before callback executed

console.log($.unify.data.assets); // This is empty, even on global variable

If so the $.unify.data.assets is not changed when you did a console.log so it's blank.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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