You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Adding a method named 'inspect' to an object does not work as expected #1798
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
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?
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
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!
Activity
bnoordhuis commentedon May 26, 2015
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.logdoes), that it needs to be kind of inspect method thatutil.format()expects.If you think the documentation could be better, can you file a pull request with suggestions?
toddself commentedon May 26, 2015
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:
inspectmethod to an object will cause unintended consequencesinspectmethod to an object could print out a warning in the REPL that you're likely not going to get what you expectWorking on a documentation PR right now.
Fishrock123 commentedon Jun 16, 2015
@toddself did you run into anything tricky while trying to document it?
chrisdickinson commentedon Jul 8, 2015
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!
doc: note about custom inspect functions