How to display the current state of an object in console.log?
P粉868586032
P粉868586032 2023-08-22 13:37:22
0
2
418

In Safari without any add-ons (and indeed in most other browsers), console.log will show the final state of the object during execution, not ;console.logThe status when called.

I have to clone the object in order to output it via console.log to get the status of the object at that line of code.

Example:

var test = {a: true} console.log(test); // {a: false} test.a = false; console.log(test); // {a: false}


P粉868586032
P粉868586032

reply all (2)
P粉473363527

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));
    P粉464113078

    I think you are looking forconsole.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)));

      Latest Downloads
      More>
      Web Effects
      Website Source Code
      Website Materials
      Front End Template
      About us Disclaimer Sitemap
      php.cn:Public welfare online PHP training,Help PHP learners grow quickly!