chrome对象console.log中的怪异行为 [英] Weird behaviour in chrome object console.log

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

问题描述

当我尝试在Chrome中使用console.log对象时,它会在console.log的第一行中指出(您在其中看到Object {small summary here}对象,我的posts数组的长度为0(posts:Array [0 ])。

When I try to console.log an object in Chrome, it states in the first line of the console.log (where you see Object { small summary here } that my posts array is of length 0 (posts: Array[0]).

但是,当我展开该帖子时,它显示其中包含27个项目(这是我所需要的)。

However when I expand the post it shows that it has 27 items in it (which is what I need).

这发生了对我来说是随机的,我不知道为什么会这样,有人曾经经历过吗?

This happens to me randomly and I got no idea why it is happening, anybody experienced this before?

截屏:

更新:这在所有浏览器中都会发生,因此它可能与Chrome无关

Update: This happens in all browsers, so it is probably not chrome related

推荐答案

调试器无法知道对象是否已更改,这就是为什么 posts 属性的呈现(在您的示例中)尚未更新的原因,即使调试器能够知道何时更改属性(每次(以及所有日志记录)都对其进行更新)会

The debugger can't know if an object has been altered, this is why the posts attribute's rendering (in your example) has not been updated. Even if the debugger would be able to know when a attribute has been changed updating it every time (and all "loggings") would be too expensive.

因此调试器仅在显式访问属性时才检查属性。

So the debugger will check the attribute only when accessing it explicitly.

Chrome在此case仅会执行一次:

Chrome in this case will do this even only once:

p = []; window.x = {x: p}; 
Object {x: Array[0]}
x: Array[0]
__proto__: Object
x.x.push(1);
1
x.x.push(2);
2

Klicking x ,数组更新

Klicking x, the Array updates

p = []; window.x = {x: p}; 
Object {x: Array[2]}
x: Array[2]
   0: 1
   1: 2
   length: 2
   __proto__: Array[0]
__proto__: Object

向数组中添加一项,然后再次切换 x ,大小和条目保持不变

Adding one item more to the array and toggleing x again, the size and entries remain

x.x.push(3)
3


x: Array[2]
   0: 1
   1: 2
   length: 2
   __proto__: Array[0]

我认为记录器没有必要更新对象值,因为可变手表已经具有此功能。在这里您可以始终更新变量的当前值。

In my opinion it's not necessary for the logger to update the object value since the variable watch has this function already. There you can always update the current value of a variable.

此功能在Firebug和Chrome中有效。这是Chrome的示例:

This works in Firebug and Chrome. Here's an example for Chrome:

这篇关于chrome对象console.log中的怪异行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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