简体   繁体   中英

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.

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.

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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