Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 952
Grub Avatar asked Nov 27 '11 05:11

Grub


1 Answers

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.

like image 96
jfriend00 Avatar answered Oct 19 '22 23:10

jfriend00