@ -275,18 +275,19 @@ The predefined color codes are: `white`, `grey`, `black`, `blue`, `cyan`,
Color styling uses ANSI control codes that may not be supported on all
Color styling uses ANSI control codes that may not be supported on all
terminals.
terminals.
### Custom `inspect()` function on Objects
### Custom inspection functions on Objects
<!-- type=misc -->
<!-- type=misc -->
Objects may also define their own `inspect(depth, opts)` function that
Objects may also define their own `[util.inspect.custom](depth, opts)`
`util.inspect()` will invoke and use the result of when inspecting the object:
(or, equivalently `inspect(depth, opts)` ) function that `util.inspect()` will
invoke and use the result of when inspecting the object:
```js
```js
const util = require('util');
const util = require('util');
const obj = { name: 'nate' };
const obj = { name: 'nate' };
obj.inspect = function(depth) {
obj[util .inspect.custom] = function(depth) {
return `{${this.name}}` ;
return `{${this.name}}` ;
};
};
@ -294,13 +295,28 @@ util.inspect(obj);
// "{nate}"
// "{nate}"
```
```
Custom `inspect(depth, opts)` functions typically return a string but may
Custom `[util. inspect.custom] (depth, opts)` functions typically return a string
return a value of any type that will be formatted accordingly by
but may return a value of any type that will be formatted accordingly by
`util.inspect()` .
`util.inspect()` .
```js
```js
const util = require('util');
const util = require('util');
const obj = { foo: 'this will not show up in the inspect() output' };
obj[util.inspect.custom] = function(depth) {
return { bar: 'baz' };
};
util.inspect(obj);
// "{ bar: 'baz' }"
```
A custom inspection method can alternatively be provided by exposing
an `inspect(depth, opts)` method on the object:
```js
const util = require('util');
const obj = { foo: 'this will not show up in the inspect() output' };
const obj = { foo: 'this will not show up in the inspect() output' };
obj.inspect = function(depth) {
obj.inspect = function(depth) {
return { bar: 'baz' };
return { bar: 'baz' };
@ -330,6 +346,14 @@ util.inspect.defaultOptions.maxArrayLength = null;
console.log(arr); // logs the full array
console.log(arr); // logs the full array
```
```
### util.inspect.custom
<!-- YAML
added: REPLACEME
-->
A Symbol that can be used to declare custom inspect functions, see
[Custom inspection functions on Objects][].
## Deprecated APIs
## Deprecated APIs
The following APIs have been deprecated and should no longer be used. Existing
The following APIs have been deprecated and should no longer be used. Existing
@ -807,6 +831,7 @@ similar built-in functionality through [`Object.assign()`].
[semantically incompatible]: https://github.com/nodejs/node/issues/4179
[semantically incompatible]: https://github.com/nodejs/node/issues/4179
[`util.inspect()`]: #util_util_inspect_object_options
[`util.inspect()`]: #util_util_inspect_object_options
[Customizing `util.inspect` colors]: #util_customizing_util_inspect_colors
[Customizing `util.inspect` colors]: #util_customizing_util_inspect_colors
[Custom inspection functions on Objects]: #util_custom_inspection_functions_on_objects
[`Error`]: errors.html#errors_class_error
[`Error`]: errors.html#errors_class_error
[`console.log()`]: console.html#console_console_log_data
[`console.log()`]: console.html#console_console_log_data
[`console.error()`]: console.html#console_console_error_data
[`console.error()`]: console.html#console_console_error_data