Variable in Console shown as reference rather than copy of variable

23 views
Skip to first unread message

Sascha M

unread,
Mar 21, 2018, 7:01:06 AM3/21/18
to Chrome DevTools
When debugging JS code, I frequently use:

console.log(obj);

to log the current contents of a variable to the Console. Today I realized that the variable shown in the Console output does not contain the contents of the variable as it was when the variable was added to Console but that it shows the most recent contents; if it was changed after the console.log() call, expanding the variable (i.e. in case of object or array variables) will show the contents as it is right now. In terms of Visual Basic I would say the variable is added "ByRef" rather than "ByVal" to the Console.

Is it possible to change this behavior?

Thanks and best regards,

Sascha

PhistucK

unread,
Mar 21, 2018, 7:26:52 AM3/21/18
to Google Chrome Developer Tools
I do not think so. Manually, you can enter instead -
console.log(JSON.stringify(obj))
And it will give you the object without circular (like methods and such) properties.

Not exactly the same, but still.
If you need the methods as well, you will need for (x in obj) {...} and recursion and...


PhistucK

--
You received this message because you are subscribed to the Google Groups "Chrome DevTools" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-chrome-developer-tools+unsub...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-chrome-developer-tools/d6031459-9613-472f-8aa0-b732b2bab772%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Kayce Basques

unread,
Apr 5, 2018, 7:21:34 PM4/5/18
to Chrome DevTools
You are correct, it logs objects by reference, not by value. There's currently no way to change that behavior. Phistuck described the workarounds. You might consider saving your custom logging functions as Snippets.
Reply all
Reply to author
Forward
0 new messages