console.log()在值实际更改之前显示变量的更改值 [英] console.log() shows the changed value of a variable before the value actually changes

查看:265
本文介绍了console.log()在值实际更改之前显示变量的更改值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这一点代码。我们制作一份A的副本并将其命名为C.当A变更时C保持不变。
var C = A;
console.log(C); // 1
A ++;
console.log(C); // 1

但是当A是一个数组时,我们有一个不同的思路。不仅C会改变,而且会在我们触碰到A之前改变。

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

有人可以解释第二个例子中发生了什么吗?

解决方案

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.

这篇关于console.log()在值实际更改之前显示变量的更改值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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