Modifying data in arrays and console.log problems in Js

this is a question of modifying array and console.log
Why does console.log output data results that change after him?

    let obj = {id: 1};
    console.log(obj); //: {obj: 1}
    obj.id = 666; 
    console.log(obj); //: {obj: 666}  
Are the objects in the

array so special? Faster than the previous code? What exactly is the principle?

in fact, this problem has not been found in many places, mainly because it is not very good at expressing it. Similar can not be found, the answer is generally taught you how to change the array elements of these operations.
but this question about the output modification order has not been found.

however, I see an answer in Stack Overflow
that the modification of the object tree in the array is faster than that of console.log, because it is in English, so the translation may be a little different.

ask all bosses to answer.

Dec.20,2021

is not that complicated. I guess it is console.log () when it encounters an object, it expands and displays the first layer, your second piece of code, by default, as follows:
clipboard.png
console.log():
clipboard.png
:
clipboard.png
console.log():
clipboard.png

:chrome devtools console console.log js {...}Array(2) console.log()


console
console

if you look at this picture, you should understand something

.

has nothing to do with console.log. You need to know first that the reference object


this is the console mechanism of chrome;
console.log () is executed by Synchronize, but the output in the chrome console is a snapshot of the object. When you open that snapshot in the console, it goes to get the contents of the object address, so it will display the modified results.
the second case you mentioned is the same.


  • if you think too much about this, it has nothing to do with the language. It must be executed sequentially.
  • just print a reference to an object, just like Schrodinger's cat, its content is uncertain if you don't open it, because the content is
  • that can be changed by later code.
  • teach you a method: first output an array, and then change the array after a few seconds of timing. You will find that clicking on the printed array before the timer changes to view the contents of the array is the original array. If you wait for the timer to change the contents of the array and then open the array, you will see the changed content
  • .
  • you can understand that the initial acronym printed by console is just a reference. Only by clicking on the array will you find out the specific contents of the array and display it on the browser. At this time, the print is a string. Even if the logic changes later, it will not change
  • .

you can take a look at the answer https://bbs.csdn.net/topics/3.
uses JSON.stringify conversion, and then turns back, it can have a similar solidification effect


console.log () has lazy evaluation problem.

Menu