简体   繁体   English

console.log()显示相同对象属性的矛盾值

[英]console.log() showing contradictory values for the same object property

I think i might be going mad. 我想我可能会发疯。

I use console.log() to see the state of an object and then on the next line do a console.log() on a particular property of the same object and get different values for each. 我使用console.log()来查看对象的状态,然后在下一行上对同一对象的特定属性执行console.log(),并为每个对象获取不同的值。

The code i'm using is: 我正在使用的代码是:

console.log(this.pictures.Items[pic].val);

for(var i in this.pictures.Items[pic].val) {
    console.log("property: %s, value: %s", i, this.pictures.Items[pic].val[i] );
}

and firebug outputs: 和萤火虫输出:

Picture { isLoaded=true, isSelected=false, img_src="imgs/image1.jpg", more...}

property: isLoaded, value: false
...more properties

as you can see, 'isLoaded' is true when logging the object itself but false when logging the property. 如您所见,“isLoaded”在记录对象本身时为true,在记录属性时为false。

I have tried logging the object again after just in case, and it is true again. 为了以防万一,我已经尝试再次记录该对象,它再次成为现实。

Does anyone know what is happening here? 有谁知道这里发生了什么?

Thanks 谢谢

Rich 丰富

I'm not entirely sure if this is what is happening to you or not, but console.log() seems to have some issues in some browsers where doing a console.log() on a value or using an index variable that is changing or being iterated in an array does not always work properly. 我不完全确定这是不是你发生了什么,但是console.log()似乎在某些浏览器中遇到了一些问题,这些浏览器对某个值执行console.log()或使用正在更改的索引变量或者在数组中迭代并不总是正常工作。

My guess is that it has something to do with things getting marshalled between process boundaries and perhaps a delay in the actual evaluation of the logging expression until a time in which the actual object or index being used or referenced has changed. 我的猜测是,它与在进程边界之间进行编组的事情有关,可能是对日志记录表达式的实际评估的延迟,直到使用或引用的实际对象或索引发生更改为止。 I've seen this issue in Chrome for sure - don't know about Firefox. 我确实在Chrome中看过这个问题 - 不知道Firefox。

You should be able to work around this particular issue by using string math to build your final string. 您应该能够通过使用字符串数学来构建最终字符串来解决此特定问题。 If only the final string is passed to console.log() where everything is fully evaluated, then this issue would not impact the output. 如果只将最终字符串传递给console.log() ,其中所有内容console.log()完全评估,则此问题不会影响输出。

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

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