Skip to content

Instantly share code, notes, and snippets.

var print = function( o, maxLevel, level )
{
if ( typeof level == "undefined" )
{
level = 0;
}
if ( typeof maxlevel == "undefined" )
{
maxLevel = 0;
}
@felthy
felthy / javascript-debug-printObject-function.js
Created April 21, 2015 06:27
Recursively dump a javascript object into a string, for when you don't have a debugger
/**
* When investigating a selenium test failure on a remote headless browser that couldn't be reproduced
* locally, I wanted to add some javascript to the site under test that would dump some state to the
* page (so it could be captured by Selenium as a screenshot when the test failed). JSON.stringify()
* didn't work because the object declared a toJSON() method, and JSON.stringify() just calls that
* method if it's present. This was a Moment object, so toJSON() returned a string but I wanted to see
* the internal state of the object instead.
*
* So, this is a rough and ready function that recursively dumps any old javascript object.
*/