How to display the current state of an object in console.log?
P粉868586032
2023-08-22 13:37:22
<p>In Safari without any add-ons (and indeed in most other browsers), <code>console.log</code> will show the final state of the object during execution, not <code> ;console.log</code>The status when called. </p>
<p>I have to clone the object in order to output it via <code>console.log</code> to get the status of the object at that line of code. </p>
<p><strong>Example:</strong></p>
<pre class="brush:php;toolbar:false;">var test = {a: true}
console.log(test); // {a: false}
test.a = false;
console.log(test); // {a: false}</pre>
<p><br /></p>
If I want to see its status at the time of recording, I usually convert it to a JSON string.
I think you are looking for
console.dir()
.console.log()
cannot achieve the function you want, because it prints a reference to the object, and it has changed when you open it.console.dir
The attribute directory of the object will be printed when called.The JSON idea below is a good one; you could even go ahead and parse the JSON string and get a browsable object, like .dir() would give you:
console.log(JSON.parse(JSON.stringify(obj)));