Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

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

Closed
wants to merge 1 commit into from

3 participants

@tj
tj commented

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
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) {
@TooTallNate Owner

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

@tj
tj added a note

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
Owner

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
Owner

@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 referenced this pull request from a commit in TooTallNate/node
@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
Owner

Superseded by #4684.

@TooTallNate TooTallNate referenced this pull request from a commit in TooTallNate/node
@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
Commits on Feb 8, 2012
  1. @tj
This page is out of date. Refresh to see the latest.
Showing with 5 additions and 3 deletions.
  1. +1 −1  lib/console.js
  2. +4 −2 lib/util.js
View
2  lib/console.js
@@ -38,7 +38,7 @@ exports.error = exports.warn;
exports.dir = function(object) {
- process.stdout.write(util.inspect(object) + '\n');
+ process.stdout.write(util.inspect(object, null, null, null, true) + '\n');
};
View
6 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) {
@TooTallNate Owner

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

@tj
tj added a note

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
var ctx = {
showHidden: showHidden,
+ bypassInspect: bypassInspect,
seen: [],
stylize: colors ? stylizeWithColor : stylizeNoColor
};
@@ -157,7 +158,8 @@ function formatValue(ctx, value, recurseTimes) {
// Filter out the util module, it's inspect function is special
value.inspect !== exports.inspect &&
// Also filter out any prototype objects using the circular check.
- !(value.constructor && value.constructor.prototype === value)) {
+ !(value.constructor && value.constructor.prototype === value) &&
+ !ctx.bypassInspect) {
return value.inspect(recurseTimes);
}
Something went wrong with that request. Please try again.