console.log() showing contradictory values for the

2020-07-10 05:06发布

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

1条回答
够拽才男人
2楼-- · 2020-07-10 05:44

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.

查看更多
登录 后发表回答