Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

Added support for `.inspect()` to return other objects #2711

Closed
wants to merge 1 commit into
from

Conversation

Projects
None yet
5 participants

tj commented Feb 8, 2012

not the greatest example but it's often useful to ignore some "private" properties or very large ones that are not really relevant.

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

function UserCollection() {
  var users = [];
  this.push = function(user){ users.push(user); };
  this.inspect = function(){ return users; };
}

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);

will output:

[ { first: 'tobi', last: 'ferret' },
  { first: 'loki', last: 'ferret' },
  { first: 'jane', last: 'ferret' } ]

I had added the constructor name like [User { first: 'tobi' }] etc but with [ all over the place it gets really difficult to read although I think that would be useful in general.

Member

bnoordhuis commented Feb 8, 2012

LGTM. One or two tests would be nice. Can another committer chime in?

tj commented Feb 8, 2012

I'll add a different commit for this but I forgot to mention that I think console.dir() could be more like the browser and ignore .inspect() so that if you are the author of the library or just someone debugging you can bypass inspection and view the "raw" objects

Member

bnoordhuis commented Feb 8, 2012

You may want to open a separate issue for that. I don't have a strong opinion on the subject but others may.

tj commented Feb 8, 2012

yup I will. If another committer is ok with this I'll add the tests

koichik commented Feb 25, 2012

@visionmedia Sorry for the delay, LGTM. Please add the tests.

ashb commented Apr 8, 2012

Does the hidden flag (which isn't passed along yet either mind, see #2443) and Object.defineProperties not already provide a way to do this:

function User(first, last) {
  this.first = first;
  this.last = last;
  this._id = 12345;
  this._role = 'admin';
  Object.defineProperties( this, {
    _id: { enumerable: false }
  , _role: { enumerable: false } 
  } );
}

Behaves like this

> console.log(new User('tobi', 'ferret'))
{ first: 'tobi', last: 'ferret' }
> console.log( require('util').inspect( new User('tobi', 'ferret'), true) )
{ last: 'ferret', [_role]: 'admin', [_id]: 12345, first: 'tobi' }

I want this as well. The test from e859271 is good, but stringifying the result unconditionally doesn't make much sense. The behavior in this patch is much better in my opinion.

Currently:

$ node
> var o = { inspect: function () { return this } }
undefined
> o
[object Object]     <---- helpful never

Oh, this needs a rebase and a test of its own though.

TooTallNate added a commit to TooTallNate/node that referenced this pull request Jan 30, 2013

util: custom `inspect()` method may return an Object
This is more like how `JSON.stringify()` works.
Closes #2711.

TooTallNate added a commit to TooTallNate/node that referenced this pull request Mar 12, 2013

util: custom `inspect()` method may return an Object
This is more like how `JSON.stringify()` works.
Closes #2711.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment