Google Chrome console.log无序? [英] Google Chrome console.log out of sequence?

查看:195
本文介绍了Google Chrome console.log无序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以解释以下两个输出吗?



代码1:

 的console.log(itemsAry); 
// loadNextItem();
函数loadNextItem(){
var item = itemsAry.shift();
console.log(item);

结果:

  [cat-53,cat-57,cat-51,cat-10,cat-55,cat-56 5,猫-50,猫-3,猫-54,猫-52,猫-9,猫-8,猫-4,猫-58 ,cat-6,cat-7] 

(如预期)。



代码2:

  console.log(itemsAry); 
loadNextItem();
函数loadNextItem(){
var item = itemsAry.shift();
console.log(item);

结果:

  [cat-57,cat-51,cat-10,cat-55,cat-56,cat-5 50,猫-3,猫-54,猫-52,猫-9,猫-8,猫-4,猫-58,猫-6 ,cat-7] 

cat-53

注意猫-53已经从最初的数组PRIOR转移到 console.log()输出,该输出应该在 shift 操作发生。我可能如何?或者我错过了什么?



编辑:它变得更糟:

 的console.log(itemsAry); 
loadNextItem(); loadNextItem(); loadNextItem(); loadNextItem();
函数loadNextItem(){
var item = itemsAry.shift();
console.log(item);
console.log(itemsAry);

结果:

  [cat-55,cat-56,cat-5,cat-50,cat-3,cat-54 52,猫-9,猫-8,猫-4,猫-58,猫-6,猫-7] 
cat-53
[猫-55,猫-56,猫-5,猫-50,猫-3,猫-54,猫-52,猫-9 cat-8,cat-4,cat-58,cat-6,cat-7]
cat-57
[cat-55, 56,猫5,猫-50,猫-3,猫-54,猫-52,猫-9,猫-8,猫-4 ,猫-58,猫-6,猫-7]
猫-51
[猫-55,猫-56,猫-5猫-50,猫-3,猫-54,猫-52,猫-9,猫-8,猫-4,猫-58 6,cat-7]
cat-10

经过FireFox测试后,它似乎专门针对Google Chrome问题。 FF输出:

  [cat-53,cat-57,cat-51,cat-10 ,猫-55,猫-56,猫-5,猫-50,猫-3,猫-54,猫-52,猫-9 cat-8,cat-4,cat-58,cat-6,cat-7] 
cat-53
[cat-57,cat -51,猫-10,猫-55,猫-56,猫-5,猫-50,猫-3,猫-54,猫-52 ,猫-9,猫-8,猫-4,猫-58,猫-6,猫-7]
cat-57
[ 猫-51,猫-10,猫-55,猫-56,猫-5,猫-50,猫-3,猫-54,猫-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,猫-56,猫-5,猫-50,猫-3,猫-54,猫-52,猫-9,猫-8 ,猫-4,猫-58,猫-6,猫-7]

输出如预期...

解决方案

console.log 总是有点迟到,你不能当涉及到对象时,请指望它。只有原语(字符串等)将直接工作。前者在内存中只有一个实例,所以当控制台获取数据时,它可能已经改变了。



当然,这取决于你使用哪个控制台,重新使用,但我经常在Chrome上遇到这种情况。



这是一个在Firebug上经历过的人。


Can someone explain the following two outputs?

Code 1:

console.log(itemsAry);
//loadNextItem();
function loadNextItem(){
    var item = itemsAry.shift();
    console.log(item);
}

Result:

["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"]

(as expected).

Code 2:

console.log(itemsAry);
loadNextItem();
function loadNextItem(){
    var item = itemsAry.shift();
    console.log(item);
}

Result:

["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

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?

EDIT: it gets worse:

console.log(itemsAry);
loadNextItem(); loadNextItem(); loadNextItem(); loadNextItem();
function loadNextItem(){
    var item = itemsAry.shift();
    console.log(item);
    console.log(itemsAry);
}

Result:

["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

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"]

Output as expected...

解决方案

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.

Of course, it depends on which console you're actually using, but I'm frequently experiencing this on Chrome.

Here is someone who experienced this on Firebug.

这篇关于Google Chrome console.log无序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆