Chrome开发人员工具中奇怪的console.log行为 [英] Bizarre console.log behaviour in Chrome Developer Tools

查看:105
本文介绍了Chrome开发人员工具中奇怪的console.log行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能存在重复:


打开Chrome开发者工具并输入:

var a = []; console.log(a); a。 push(1); console.log(a);



你会希望这会输出类似于

  [] 
[1]

但是,它输出

  [1] 
[1]


$ b


$ b

var a的行为是一样的= []; console.log(a); a [0] = 1; console.log(a);



任何人都可以解释这种行为?



在OS X上运行Chrome。在32位Windows 7上有相同的行为。
$ b 编辑:无论是否是sta,行为都是一样的帐单是否在同一行上。



放置

我们在一行中简单地提供了它们以便于测试。  var a = []; 
console.log(a);
.p.p(1);
console.log(a);

在一个文件中运行它会产生相同的行为。



编辑x 2
请参阅:
http:// jsfiddle。 net / 9N4A6 / ,如果你不想做一个文件测试。

  var a = [];的console.log(a.toString()); a.push(1);的console.log(a.toString()); 

这并不是说评估顺序很奇怪,我敢打赌,但是将对象转换为可打印的表单发生在所有语句全部执行后,即在Chrome准备好实际转储日志时。


Possible Duplicate:
Is Chrome's JavaScript console lazy about evaluating arrays?

Open up Chrome Developer Tools and type in:

var a = [];console.log(a);a.push(1);console.log(a);

You would expect this to output something like

[]
[1]

But instead it outputs

[1]
[1]

The behaviour is the same for

var a = [];console.log(a);a[0] = 1;console.log(a);

Can anyone explain this behaviour?

Running Chrome on OS X. Same behaviour on 32bit Windows 7.

EDIT: The behaviour is the same regardless of whether the statements are on the same line or not. I have simply provided them on a single line to make it easy to test.

Putting

var a = [];
console.log(a);
a.push(1);
console.log(a);

in a file then running it yields the same behaviour.

EDIT x 2 See: http://jsfiddle.net/9N4A6/ if you don't feel like making a file to test.

解决方案

Try this instead:

var a = []; console.log(a.toString()); a.push(1); console.log(a.toString());

It's not that the order of evaluation is strange, I bet, but that the conversion of the objects to printable form happens after the statements are all executed, at the point when Chrome is ready to actually dump out the log.

这篇关于Chrome开发人员工具中奇怪的console.log行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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