Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

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

Closed
wants to merge 1 commit into from

Conversation

tj
Copy link

@tj 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.

@bnoordhuis
Copy link
Member

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

@tj
Copy link
Author

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

@bnoordhuis
Copy link
Member

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

@tj
Copy link
Author

tj commented Feb 8, 2012

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

@koichik
Copy link

koichik commented Feb 25, 2012

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

@ashb
Copy link

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' }

@TooTallNate
Copy link

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

@TooTallNate
Copy link

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

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants