简体   繁体   English

Google Chrome console.log() 与对象和数组不一致

[英]Google Chrome console.log() inconsistency with objects and arrays

I was helping a colleague debug some code today and I noticed a strange behavior with console.log() in Google Chrome:我今天正在帮助一位同事调试一些代码,我注意到谷歌浏览器中的console.log()有一个奇怪的行为:

It appears that if you:看来,如果你:

  1. Create a nested array (eg, [[345,"test"]])创建一个嵌套数组(例如,[[345,"test"]])

  2. Log the array to the console with console.log() .使用console.log()将数组记录到控制台。

  3. Modify one of the inner array values, then console.log() will output the later value -- not the values of the array at the time the console.log() was executed.修改内部数组值之一,然后console.log()将输出后面的值——而不是执行console.log()时数组的值。

JavaScript : JavaScript :

var test = [[2345235345,"test"]]
console.log(test);
test[0][0] = 1111111;
// outputs: [[1111111,"test"]]

var testb = {};
testb.test = "test";
console.log(testb);
testb.test = "sdfgsdfg";
// outputs: {"testb":"test"}


var testc = ["test","test2"];
console.log(testc);
testc[0] = "sdxfsdf";
// outputs: ["test","test2"]

JSFiddle Example JSFiddle 示例

This behavior does not happen in Firefox.这种行为在 Firefox 中不会发生。

Also to note, if I stepped through his code line by line in the Chrome debugger, then console.log() would output the correct values.还要注意的是,如果我在 Chrome 调试器中逐行浏览他的代码,那么console.log()将输出正确的值。

Is there an explanation for this strange phenomenon or is it just a bug with Google Chrome?这种奇怪的现象是否有解释,或者只是谷歌浏览器的错误?

EDIT:编辑:

I've narrowed down the steps to reproduce the inconsistent console.log() behavior:我缩小了重现不一致console.log()行为的步骤:

If you add this script to your page:如果您将此脚本添加到您的页面:

var greetings=['hi','bye'];
console.log(greetings);
setTimeout(function(){
    greetings.push('goodbye');
},3000);

and open it in a new window with the Chrome console window already open , then the console.log() output will be different than if you load the page with the console window closed .并在 Chrome控制台窗口已经打开的新窗口中打开它,那么console.log()输出将与在控制台窗口关闭的情况下加载页面不同。 Here's a JSFiddle that demonstrates that .这是一个 JSFiddle,它演示了.

In the first case, with the console window already open, console.log() will output the current value of the array (ie, two items).在第一种情况下,控制台窗口已经打开, console.log()将输出数组的当前值(即两个项目)。

In the second case, with the console window initially closed and opened only after the page loads, console.log() will output the later values of the array (ie, three items).在第二种情况下,控制台窗口最初关闭并仅页面加载打开, console.log()将输出数组的后console.log() (即三个项目)。

Is this a bug in Google Chrome's console.log() functionality?这是 Google Chrome 的console.log()功能中的错误吗?

After a lot of digging, I found that this has been reported as a bug, fixed in Webkit, but apparently not yet pulled into Google Chrome.经过大量挖掘,我发现这已被报告为一个错误,已在 Webkit 中修复,但显然尚未拉入谷歌浏览器。

As far as I can tell, the issue was originally reported here: https://bugs.webkit.org/show_bug.cgi?id=35801 :据我所知,这个问题最初是在这里报告的: https : //bugs.webkit.org/show_bug.cgi?id=35801

Description From mitch kramer 2010-03-05 11:37:45 PST描述来自米奇克莱默 2010-03-05 11:37:45 PST

1) create an object literal with one or more properties 1) 创建一个具有一个或多个属性的对象字面量

2) console.log that object but leave it closed (don't expand it in the console) 2)console.log 该对象但保持关闭(不要在控制台中展开它)

3) change one of the properties to a new value 3) 将属性之一更改为新值

now open that console.log and you'll see it has the new value for some reason, even though it's value was different at the time it was generated.现在打开那个console.log,你会看到由于某种原因它有新的值,即使它的值在它生成的时候是不同的。

I should point out that if you open it, it will retain the correct value if that wasn't clear.我应该指出,如果您打开它,如果不清楚,它将保留正确的值。

Response from a Chromium developer: Chromium 开发人员的回应:

Comment #2 From Pavel Feldman 2010-03-09 06:33:36 PST评论 #2 来自 Pavel Feldman 2010-03-09 06:33:36 PST

I don't think we are ever going to fix this one.我认为我们永远不会解决这个问题。 We can't clone object upon dumping it into the console and we also can't listen to the object properties' changes in order to make it always actual.我们不能在将对象转储到控制台时克隆它,我们也不能监听对象属性的变化以使其始终真实。

We should make sure existing behavior is expected though.我们应该确保现有的行为是预期的。

A fix was implemented two and a half years later on August 9th, 2012 for Webkit ( http://trac.webkit.org/changeset/125174 ), but it does not appear to have made it into Chrome yet.一个修复程序两年半后实施了2012年8月9日进行的Webkit( http://trac.webkit.org/changeset/125174 ),但它似乎没有我们已经进入Chrome浏览器呢。

As of today, dumping an object (array) into console will result in objects' properties being read upon console object expansion (ie lazily).截至今天,将对象(数组)转储到控制台将导致在控制台对象扩展(即懒惰)时读取对象的属性。 This means that dumping the same object while mutating it will be hard to debug using the console.这意味着在改变它的同时转储相同的对象将很难使用控制台进行调试。

This change starts generating abbreviated previews for objects / arrays at the moment of their logging and passes this information along into the front-end.此更改在记录对象/数组时开始为它们生成简短的预览,并将此信息传递到前端。 This only happens when the front-end is already opened, it only works for console.log(), not live console interaction.这仅在前端已经打开时发生,它仅适用于 console.log(),不适用于实时控制台交互。

I found a workaround for this bug/feature.我找到了此错误/功能的解决方法。

console.log(JSON.parse(JSON.stringify(myObject)));

Edit: Unfortunately this won't work for non-primitive values like functions.编辑:不幸的是,这不适用于非原始值,如函数。 Use another clone utility here.在此处使用另一个克隆实用程序。

jQuery example: jQuery 示例:

console.log($.extend({}, myObject));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM