Wesley Wesley - 4 years ago 266
Javascript Question

console.log object at current state

will show the object at the last state of execution, not at the state when
was called.

I have to clone the object just to output it via
to get the state of the object at that line.

How can I change the default behavior of console.log? (Error console in safari, no add-on)


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

Answer Source

I think you're looking for console.dir().

console.log() doesn't do what you want because it prints a reference to the object, and by the time you pop it open, it's changed. console.dir prints a directory of the properties in the object at the time you call it.

The JSON idea below is a good one; you could even go on to parse the JSON string and get a browsable object like what .dir() would give you:


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download