且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

console.log()在值实际更改之前显示变量的更改值

更新时间:2022-10-15 16:11:40

Pointy的回答有很好的信息,但这不是这个问题的正确答案。



OP描述的行为是2010年3月首次报告的bug中的一部分,在8月针对Webkit进行了修补2012年,但截至撰写本文时尚未纳入谷歌浏览器。该行为取决于对象文本传递给 console.log()时是否打开控制台调试窗口打开关闭 / code>。



原始错误报告摘录( https://bugs.webkit.org/show_bug.cgi?id=35801 ):


描述从mitch kramer 2010-03-05 11:37:45 PST



1)创建一个具有一个或多个属性的对象字面值


$ b $ 2)console.log该对象但保持关闭状态(不要在控制台中展开它)

3)更改一个现在打开console.log,你会发现它有一个新的值,因为某些原因,即使它的值不同于它的值。它是生成的时间。



我应该指出,如果你打开它,它会保留正确的值,如果这个WA


来自Chromium开发者的回应:


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



我认为我们不打算解决这个问题一。我们无法在将对象转储到控制台时克隆对象,并且我们也无法侦听对象属性的更改以使其始终处于真实状态。



我们应该确保现有的行为是预期的。


随之而来的是抱怨,最终导致错误修复。



2012年8月实施的补丁更新日志记录( http:// trac。 webkit.org/changeset/125174 ):

lockquote
截至今天,将一个对象(数组)转储到控制台将导致对象的属性是
在控制台对象扩展时读取(即延迟)。这意味着在
变异的情况下转储同一个对象将很难使用控制台进行调试。



这种变化开始在对象/数组中生成缩略预览他们的
记录的时刻,并将这些信息传递到前端。这只发生在前端
已经打开时,它只适用于console.log(),而不是实时控制台交互。



This bit of code I understand. We make a copy of A and call it C. When A is changed C stays the same

var A = 1;
var C = A;
console.log(C); // 1
A++;
console.log(C); // 1

But when A is an array we have a different sitiuation. Not only will C change, but it changes before we even touch A

var A = [2, 1];
var C = A;
console.log(C); // [1, 2]
A.sort();
console.log(C); // [1, 2]

Can someone explain what happened in the second example?

Pointy's answer has good information, but it's not the correct answer for this question.

The behavior described by the OP is part of a bug that was first reported in March 2010, patched for Webkit in August 2012, but as of this writing is not yet integrated into Google Chrome. The behavior hinges upon whether or not the console debug window is open or closed at the time the object literal is passed to console.log().

Excerpts from the original bug report (https://bugs.webkit.org/show_bug.cgi?id=35801):

Description From mitch kramer 2010-03-05 11:37:45 PST

1) create an object literal with one or more properties

2) console.log that object but leave it closed (don't expand it in the console)

3) change one of the properties to a new value

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.

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:

Comment #2 From 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.

Much complaining ensued and eventually it led to a bug fix.

Changelog notes from the patch implemented in August 2012 (http://trac.webkit.org/changeset/125174):

As of today, dumping an object (array) into console will result in objects' properties being read upon console object expansion (i.e. 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.