Changed: `console.dir()` to bypass inspect() methods #2717

Closed
wants to merge 1 commit into from

3 participants

@tj
tj commented Feb 8, 2012

Given the following script:

function User(first, last) {
  this.first = first;
  this.last = last;
  this._id = 12345;
  this._role = 'admin';
  this.inspect = function(){
    return '#<User ' + this.first + '>'
  };
}

function UserCollection() {
  var users = [];
  this.push = function(user){ users.push(user); };
  this.inspect = function(){
    return '#<UserCollection\n  ' + users.map(function(user){
      return user.inspect();
    }).join('\n  ') + '>'
  };
}

var users = new UserCollection;
users.push(new User('tobi', 'ferret'));
users.push(new User('loki', 'ferret'));
users.push(new User('jane', 'ferret'));

console.log(users);
console.log();
console.dir(users);

before this change you get the same output of:

#<UserCollection
  #<User tobi>
  #<User loki>
  #<User jane>>

#<UserCollection
  #<User tobi>
  #<User loki>
  #<User jane>>

not so useful, to make console.dir() more like the browser in that it's more representative of what the object really contains I think this would be a really useful switch, otherwise there's no way (AFAIK) to bypass potentially confusing .inspect() implementations. With this change you'll then get:


#<UserCollection
  #<User tobi>
  #<User loki>
  #<User jane>>

{ push: [Function], inspect: [Function] }
@TooTallNate TooTallNate commented on the diff Feb 8, 2012
lib/util.js
@@ -91,9 +91,10 @@ var error = exports.error = function(x) {
* @param {Boolean} colors Flag to turn on ANSI escape codes to color the
* output. Default is false (no coloring).
*/
-function inspect(obj, showHidden, depth, colors) {
+function inspect(obj, showHidden, depth, colors, bypassInspect) {

5 args, haha. Maybe we should start with turning this into an options object.

@tj
tj added a note Feb 8, 2012

yeah haha I was going to but I guess that's for a separate patch

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

I'm down to merge this but it needs a rebase, and a test would also be good.

I also wanna address my other comment before v0.10 is released so I've opened #4085, but this should go in first.

@isaacs

@TooTallNate If we do this, then #4085 should go in first, and this should be an added option on the option object. We already have to support the existing ones in a backward-compatible way, no need to add a fifth.

@TooTallNate TooTallNate added a commit to TooTallNate/node that referenced this pull request Jan 30, 2013
@TooTallNate TooTallNate console: `console.dir()` bypasses inspect() methods
Use the `customInspect: false` option of `util.inspect()` to bypass any custom
inspect() function on the object being logged.

Closes #2717.
b46fa35
@TooTallNate

Superseded by #4684.

@TooTallNate TooTallNate added a commit to TooTallNate/node that referenced this pull request Mar 12, 2013
@TooTallNate TooTallNate console: `console.dir()` bypasses inspect() methods
Use the `customInspect: false` option of `util.inspect()` to bypass any custom
inspect() function on the object being logged.

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