Different property value is displayed when JavaScript object is expanded in Chrome console

后端 未结 2 674
不知归路
不知归路 2020-12-02 01:39

I\'m doing some JavaScript debugging with Chrome dev tools and found the following oddity.

How is it possible that date has a different value when the

2条回答
  •  执念已碎
    2020-12-02 02:25

    You should be careful with chrome console when printing objects. Please note that chrome does evaluations when printing / expanding objects as well as it does it asynchronously (meaning other code may execute in the meantime and change the object). Always try to print to String for debugging, rather than printing the object itself.

    I made a very simple example to illustrate the problem.

    console.log($('#foo')); //expected output [div#foo...] $('#foo').attr('id','hello');

    The actual output in the chrome console is:

    example image

    Try it yourself here (JSFiddle).

提交回复
热议问题