Chrome 开发者工具中的异常 console.log 行为 [英] Bizarre console.log behaviour in Chrome Developer Tools

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

问题描述

可能的重复:
Chrome 的 JavaScript 控制台是否懒得计算数组?

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

Open up Chrome Developer Tools and type in:

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

你会期望它输出类似

[]
[1]

而是输出

[1]
[1]

行为是相同的

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

谁能解释这种行为?

在 OS X 上运行 Chrome.在 32 位 Windows 7 上的行为相同.

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

EDIT:无论语句是否在同一行,行为都是相同的.我只是在一行中提供了它们,以便于测试.

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.

放置

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

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

in a file then running it yields the same behaviour.

编辑 x 2请参阅:http://jsfiddle.net/9N4A6/ 如果您不想制作文件测试.

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

推荐答案

试试这个:

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

我敢打赌,并不是求值的顺序很奇怪,而是对象到可打印形式的转换发生在语句全部执行之后,此时 Chrome 已准备好实际转储日志.

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天全站免登陆