简体   繁体   English

Firefox console.table显示错误数据

[英]Firefox console.table showing wrong data

Let's try the following javascript code: 让我们尝试以下javascript代码:

 var Matrix = [
  [1,2,3],
  [4,5,6]
 ];

  console.table(Matrix);   // print initial data
  Matrix[1][1] = 9;        // change one value
  console.table(Matrix);   // print updated data

And this is the result: 结果如下:

结果矩阵

I have tested this with Firefox 64.0 . 我已经使用Firefox 64.0对此进行了测试。 Why do both console.table -calls result to the same output? 为什么两个console.table -calls结果都输出到相同的输出?

console.log is not standardized, so the behavior is rather undefined, console.log不是标准化的,因此行为相当不确定,

The console will need to store the logged values somewhere, and it will display them on the screen. 控制台将需要将记录的值存储在某个位置,并将其显示在屏幕上。 The rendering will happen asynchronously for sure (being throttled to rate-limit updates), as will future interactions with the logged objects in the console (like expanding object properties). 渲染肯定会异步发生(被限制为限速更新),将来与控制台中已记录对象的交互(例如扩展对象属性)也将异步发生。

So the console might either clone (serialize) the mutable objects that you did log, or it will store references to them. 因此,控制台可以克隆(序列化)您记录的可变对象,也可以存储对它们的引用。 The first one doesn't work well with deep objects. 第一个不适用于深层物体。 Also, at least the initial rendering in the console will probably show the "current" state of the object, ie the one when it got logged - in your example you see Object {}. 另外,至少控制台中的初始渲染可能会显示对象的“当前”状态,即记录对象时的状态-在您的示例中,您将看到Object {}。 reminding that multi dimensional array is also an object 提醒多维数组也是一个对象

However, when you expand the object to inspect its properties further, it is likely that the console will have only stored a reference to your object and its properties, and displaying them now will then show their current (already mutated) state. 但是,当您扩展对象以进一步检查其属性时,控制台可能只存储了对您的对象及其属性的引用,现在显示它们将显示其当前(已变异)状态。

this answer is influenced by Bergis answer on this question. 此答案受此问题的伯吉斯答案的影响。

The console.table is not synchronous so before the initial content of Matrix is displayed, the update is done. console.table不是同步的,因此在显示Matrix的初始内容之前,更新已完成。 Just adding a delay like this and you'll see two values displayed correctly: 只需添加这样的延迟,您将看到两个值正确显示:

var Matrix = [
  [1, 2, 3],
  [4, 5, 6]
];

console.table(Matrix);   // print initial data
setTimeout(() => {
  Matrix[1][1] = 9;        // change one value
  console.table(Matrix);   // print updated data
}, 2000);

This is not practical of course, but looking on the mozilla website it says Note: This feature is available in Web Workers. 当然这是不切实际的,但是在mozilla网站上会显示“注意”:Web Workers中提供了此功能。 But the french translation says the feature is available via web workers which would explain the behavior you're seeing. 但是法语翻译说该功能可以通过网络工作者获得,这可以解释您所看到的行为。

A workaround would be to call console.table on a deep copy of Matrix so you can carry on modifying it with minimum impact on you final code. 一种解决方法是在Matrix的深层副本上调用console.table,以便您可以对其进行修改,而对最终代码的影响最小。

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

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