简体   繁体   English

将全局变量分配给功能参数

[英]Assign Global Variable to Function Parameter

For example: 例如:

var obj = {};

$.getJSON('data.json', function(data) {
    obj = data;
});

console.log(obj)

The console then outputs an empty object instead of whatever value data was. 然后,控制台将输出一个空对象,而不是输出任何值data

in your code 在你的代码中

$.getJSON('data.json', function(data) {
    obj = data;
});

is an asynchronous function, the code 是一个异步函数,代码

console.log(obj)

does not wait for it to stop executing and hence you cannot see anything 不等待它停止执行,因此您看不到任何东西

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

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