Skip to content

Commit d9f857d

Browse files
committed
doc: note about custom inspect functions
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 <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Michaël Zasso <[email protected]>
1 parent 0a7bf81 commit d9f857d

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

doc/api/repl.markdown

+24
Original file line numberDiff line numberDiff line change
@@ -237,3 +237,27 @@ The following key combinations in the REPL have these special effects:
237237
- `<ctrl>D` - Similar to the `.exit` keyword.
238238
- `<tab>` - Show both global and local(scope) variables
239239

240+
241+
### Customizing Object displays in the REPL
242+
243+
The REPL module internally uses
244+
[util.inspect()][], when printing values. However, `util.inspect` delegates the
245+
call to the object's `inspect()` function, if it has one. You can read more
246+
about this delegation [here][].
247+
248+
For example, if you have defined an `inspect()` function on an object, like this:
249+
250+
> var obj = { foo: 'this will not show up in the inspect() output' };
251+
undefined
252+
> obj.inspect = function() {
253+
... return { bar: 'baz' };
254+
... };
255+
[Function]
256+
257+
and try to print `obj` in REPL, it will invoke the custom `inspect()` function:
258+
259+
> obj
260+
{ bar: 'baz' }
261+
262+
[util.inspect()]: util.html#util_util_inspect_object_options
263+
[here]: util.html#util_custom_inspect_function_on_objects

0 commit comments

Comments
 (0)