Skip to content

Adding a method named 'inspect' to an object does not work as expected #1798

Closed

Description

@toddself

I understand this has probably been like this for a while but I've never created a method named inspect on an object. Just spent over two hours trying to debug this.

At the very least this should be very well documented. Right now the first link in Google for node inspect reserved is a stackoverflow answer and there is no mention of this behavior in the documentation.

Obviously running this code in Chrome, Firefox, Safari, etc works as expected.

> process.versions
{ http_parser: '2.5.0',
  node: '2.0.2',
  v8: '4.2.77.20',
  uv: '1.5.0',
  zlib: '1.2.8',
  ares: '1.10.1-DEV',
  modules: '44',
  openssl: '1.0.2a' }
> function Test(){ };
undefined
> Test.prototype.inspect = function(){ return 'hahaha';}
[Function]
> var t = new Test()
undefined
> t
hahaha

Activity

  1. bnoordhuis commented on May 26, 2015

    @bnoordhuis
    Member

    It's documented here. You are free to add an inspect method to your objects, it's only when passing it to util.format() (like e.g. console.log does), that it needs to be kind of inspect method that util.format() expects.

    If you think the documentation could be better, can you file a pull request with suggestions?

  2. toddself commented on May 26, 2015

    @toddself
    ContributorAuthor

    I think the confusion is that if you type that code into the REPL you get those results which contravenes what you'd expect in any other JS REPL.

    Ideally the node REPL would function similarly to other JS REPL consoles (principal of least surprise is always nice), but given that is probably not going to occur, ideally:

    • The REPL documentation should explain that adding an inspect method to an object will cause unintended consequences
    • Adding an inspect method to an object could print out a warning in the REPL that you're likely not going to get what you expect

    Working on a documentation PR right now.

  3. added
    utilIssues and PRs related to the built-in util module.
    on May 26, 2015
  4. added
    docIssues and PRs related to Node.js documentation.
    on May 27, 2015
  5. Fishrock123 commented on Jun 16, 2015

    @Fishrock123
    Contributor

    @toddself did you run into anything tricky while trying to document it?

  6. chrisdickinson commented on Jul 8, 2015

    @chrisdickinson
    Contributor

    It's been a few months since the last activity on this issue — closing it for now. Although this issue is closed, a PR clarifying the docs would still be welcome!

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    docIssues and PRs related to Node.js documentation.utilIssues and PRs related to the built-in util module.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions