[英]Why does console.log log a value before it's assigned?
A short example: 一个简短的例子:
self.curTabs = null;
$j.getJSON(url)
.done(function (response) {
self.curTabs = response.tabs;
_.each(self.curTabs, function (tab) {
tab.dataLoaded = true;
});
console.log(self.curTabs);
});
Logical output: [ 0: Object { dataLoaded: true, etc... }, 1: etc... ]
. 逻辑输出: [ 0: Object { dataLoaded: true, etc... }, 1: etc... ]
。
But with this example: 但是这个例子:
self.curTabs = null;
$j.getJSON(url)
.done(function (response) {
self.curTabs = response.tabs;
_.each(self.curTabs, function (tab) {
tab.dataLoaded = true;
});
console.log(self.curTabs);
_.each(self.curTabs, function (tab) {
tab.dataLoaded = false;
});
});
Illogical output: [ 0: Object { dataLoaded: false, etc... }, 1: etc... ]
. 不合逻辑的输出: [ 0: Object { dataLoaded: false, etc... }, 1: etc... ]
。
Why the variable get the value false
before I assign it? 为什么变量在赋值之前得到的值为false
?
Because console.log isn't synchronous in every implementation. 因为console.log在每个实现中都不是同步的。 That way it's queued till the main thread finished. 这样它排队直到主线完成。 In the meantime your new value is set. 在此期间,您的新值已设置。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.