Home  >  Q&A  >  body text

How to display the current state of an object in console.log?

<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>
P粉868586032P粉868586032425 days ago502

reply all(2)I'll reply

  • P粉473363527

    P粉4733635272023-08-23 12:27:07

    If I want to see its status at the time of recording, I usually convert it to a JSON string.

    console.log(JSON.stringify(a));

    reply
    0
  • P粉464113078

    P粉4641130782023-08-23 11:29:35

    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.dirThe 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)));

    reply
    0
  • Cancelreply