問題描述
有人可以解釋以下兩個(gè)輸出嗎?
Can someone explain the following two outputs?
代碼 1:
console.log(itemsAry);
//loadNextItem();
function loadNextItem(){
var item = itemsAry.shift();
console.log(item);
}
結(jié)果:
["cat-53", "cat-57", "cat-51", "cat-10", "cat-55", "cat-56", "cat-5", "cat-50", "cat-3", "cat-54", "cat-52", "cat-9", "cat-8", "cat-4", "cat-58", "cat-6", "cat-7"]
(如預(yù)期).
代碼 2:
console.log(itemsAry);
loadNextItem();
function loadNextItem(){
var item = itemsAry.shift();
console.log(item);
}
結(jié)果:
["cat-57", "cat-51", "cat-10", "cat-55", "cat-56", "cat-5", "cat-50", "cat-3", "cat-54", "cat-52", "cat-9", "cat-8", "cat-4", "cat-58", "cat-6", "cat-7"]
cat-53
請(qǐng)注意,cat-53 已從原始數(shù)組 PRIOR 轉(zhuǎn)移到 console.log()
輸出,該輸出應(yīng)該在 shift
操作之前發(fā)生地方.我怎么可能?或者我錯(cuò)過了什么?
Notice that cat-53 has been shifted from the original array PRIOR to the console.log()
output that is supposed to be occurring BEFORE the shift
operation ever takes place. How i this possible? Or what am I missing?
情況變得更糟:
console.log(itemsAry);
loadNextItem(); loadNextItem(); loadNextItem(); loadNextItem();
function loadNextItem(){
var item = itemsAry.shift();
console.log(item);
console.log(itemsAry);
}
結(jié)果:
["cat-55", "cat-56", "cat-5", "cat-50", "cat-3", "cat-54", "cat-52", "cat-9", "cat-8", "cat-4", "cat-58", "cat-6", "cat-7"]
cat-53
["cat-55", "cat-56", "cat-5", "cat-50", "cat-3", "cat-54", "cat-52", "cat-9", "cat-8", "cat-4", "cat-58", "cat-6", "cat-7"]
cat-57
["cat-55", "cat-56", "cat-5", "cat-50", "cat-3", "cat-54", "cat-52", "cat-9", "cat-8", "cat-4", "cat-58", "cat-6", "cat-7"]
cat-51
["cat-55", "cat-56", "cat-5", "cat-50", "cat-3", "cat-54", "cat-52", "cat-9", "cat-8", "cat-4", "cat-58", "cat-6", "cat-7"]
cat-10
在 FireFox 中進(jìn)行測(cè)試后,這似乎是特定于 Google Chrome 的問題.FF 輸出:
After testing in FireFox, it appears to be a Google Chrome issue specifically. FF output:
["cat-53", "cat-57", "cat-51", "cat-10", "cat-55", "cat-56", "cat-5", "cat-50", "cat-3", "cat-54", "cat-52", "cat-9", "cat-8", "cat-4", "cat-58", "cat-6", "cat-7"]
cat-53
["cat-57", "cat-51", "cat-10", "cat-55", "cat-56", "cat-5", "cat-50", "cat-3", "cat-54", "cat-52", "cat-9", "cat-8", "cat-4", "cat-58", "cat-6", "cat-7"]
cat-57
["cat-51", "cat-10", "cat-55", "cat-56", "cat-5", "cat-50", "cat-3", "cat-54", "cat-52", "cat-9", "cat-8", "cat-4", "cat-58", "cat-6", "cat-7"]
cat-51
["cat-10", "cat-55", "cat-56", "cat-5", "cat-50", "cat-3", "cat-54", "cat-52", "cat-9", "cat-8", "cat-4", "cat-58", "cat-6", "cat-7"]
cat-10
["cat-55", "cat-56", "cat-5", "cat-50", "cat-3", "cat-54", "cat-52", "cat-9", "cat-8", "cat-4", "cat-58", "cat-6", "cat-7"]
按預(yù)期輸出...
推薦答案
console.log
總是有點(diǎn)遲到",當(dāng)涉及到對(duì)象時(shí),你不能指望它.只有原語(字符串等)才能直接工作.前者在內(nèi)存中只有一個(gè)實(shí)例,所以當(dāng)控制臺(tái)獲取數(shù)據(jù)時(shí),它可能已經(jīng)改變了.
console.log
is always a little "late" and you can't count on it when it comes to objects. Only primitives (strings etc.) will work directly. Of the former there is only one instance in memory, so when the console is fetching the data it may have changed already.
當(dāng)然,這取決于您實(shí)際使用的控制臺(tái),但我經(jīng)常在 Chrome 上遇到這種情況.
Of course, it depends on which console you're actually using, but I'm frequently experiencing this on Chrome.
這里有人在螢火蟲.
這篇關(guān)于谷歌瀏覽器 console.log 亂序?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!