Skip to content

Instantly share code, notes, and snippets.

@1j01
Last active July 30, 2018 20:27
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 1j01/d024f3f7fa6b2bb61e7f to your computer and use it in GitHub Desktop.
Save 1j01/d024f3f7fa6b2bb61e7f to your computer and use it in GitHub Desktop.
Node.js hack to inspect [Object object]

Ever gotten some output on the console like [Object object], with no explanation? Can't find where it's being logged? Maybe it comes right before some ludicrous error or crash. Sometimes you just need a hack.

JavaScript

var ots = Object.prototype.toString;
Object.prototype.toString = function() { try { return JSON.stringify(this); } catch (e) { return ots.apply(this); } };

CoffeeScript

ots = Object::toString
Object::toString = -> try JSON.stringify @ catch then ots.apply @

This will let you actually see the contents of these mysterious [Object object]s.

Of course, this will screw up any libraries or code that relies on the default behaviour of Object.prototype.toString.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment