I was also interested in how to do this when I saw how beautifully Immutable.js displays objects:
var Immutable = require('immutable'); var map = Immutable.Map({ a: 1, b: 2 }); console.log(map);
After some scanning of the source code, I found that they pulled it out by adding the toString and inspect methods to the prototype object. Here is the basic idea, more or less:
function Map(obj) { this.obj = obj; } Map.prototype.toString = function () { return 'Map ' + JSON.stringify(this.obj); } Map.prototype.inspect = function () { return this.toString(); }
The presence of the toString and inspect methods means that the object will be correctly disabled in the node (using inspect ), and if necessary it will be correctly formatted as a string (using toString ).
EDIT: this only applies to node, browsers will still log out. If you do not want this, first convert it to a string, either by calling toString , or by concatenating it using another string: console.log('' + map) .
source share