Skip to content
New issue

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

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
toddself opened this issue May 26, 2015 · 4 comments
Closed
Labels
doc Issues and PRs related to the documentations. util Issues and PRs related to the built-in util module.

Comments

@toddself
Copy link
Contributor

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
@bnoordhuis
Copy link
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?

@toddself
Copy link
Contributor Author

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.

@mscdex mscdex added the util Issues and PRs related to the built-in util module. label May 26, 2015
@Fishrock123 Fishrock123 added the doc Issues and PRs related to the documentations. label May 27, 2015
@Fishrock123
Copy link
Contributor

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

@chrisdickinson
Copy link
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!

thefourtheye added a commit to thefourtheye/io.js that referenced this issue Jul 10, 2015
See: nodejs#1798

When an Object is printed in REPL, the actual representation can be
overriden by defining `inspect` method on the objects. This patch
includes a note about the same in the REPL documentation
thefourtheye added a commit that referenced this issue Jul 24, 2015
See: #1798

When an Object is printed in REPL, the actual representation can be
overriden by defining `inspect` method on the objects. This patch
includes a note about the same in the REPL documentation.

PR-URL: #2142
Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Michaël Zasso <mic.besace@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
doc Issues and PRs related to the documentations. util Issues and PRs related to the built-in util module.
Projects
None yet
Development

No branches or pull requests

5 participants