@@ -579,7 +579,7 @@ changes:
579579 codes. Colors are customizable. See [Customizing `util.inspect` colors][].
580580 **Default:** `false`.
581581 * `customInspect` {boolean} If `false`,
582- `[util.inspect.custom](depth, opts)` functions are not invoked.
582+ `[util.inspect.custom](depth, opts, inspect )` functions are not invoked.
583583 **Default:** `true`.
584584 * `showProxy` {boolean} If `true`, `Proxy` inspection includes
585585 the [`target` and `handler`][] objects. **Default:** `false`.
@@ -874,10 +874,18 @@ ignored, if not supported.
874874
875875<!-- type=misc -->
876876
877+ <!-- YAML
878+ added: v0.1.97
879+ changes:
880+ - version: REPLACEME
881+ pr-url: https://github.com/nodejs/node/pull/41019
882+ description: The inspect argument is added for more interoperability.
883+ -->
884+
877885Objects may also define their own
878- [`[util.inspect.custom](depth, opts)`][util.inspect.custom] function,
886+ [`[util.inspect.custom](depth, opts, inspect )`][util.inspect.custom] function,
879887which `util.inspect()` will invoke and use the result of when inspecting
880- the object:
888+ the object.
881889
882890```js
883891const util = require('util');
@@ -887,7 +895,7 @@ class Box {
887895 this.value = value;
888896 }
889897
890- [util.inspect.custom](depth, options) {
898+ [util.inspect.custom](depth, options, inspect ) {
891899 if (depth < 0) {
892900 return options.stylize('[Box]', 'special');
893901 }
@@ -898,8 +906,8 @@ class Box {
898906
899907 // Five space padding because that's the size of "Box< ".
900908 const padding = ' '.repeat(5);
901- const inner = util. inspect(this.value, newOptions)
902- .replace(/\n/g, `\n${padding}`);
909+ const inner = inspect(this.value, newOptions)
910+ .replace(/\n/g, `\n${padding}`);
903911 return `${options.stylize('Box', 'special')}< ${inner} >`;
904912 }
905913}
@@ -910,9 +918,9 @@ util.inspect(box);
910918// Returns: "Box< true >"
911919```
912920
913- Custom `[util.inspect.custom](depth, opts)` functions typically return a string
914- but may return a value of any type that will be formatted accordingly by
915- `util.inspect()`.
921+ Custom `[util.inspect.custom](depth, opts, inspect )` functions typically return
922+ a string but may return a value of any type that will be formatted accordingly
923+ by `util.inspect()`.
916924
917925```js
918926const util = require('util');
@@ -942,8 +950,13 @@ In addition to being accessible through `util.inspect.custom`, this
942950symbol is [registered globally][global symbol registry] and can be
943951accessed in any environment as `Symbol.for('nodejs.util.inspect.custom')`.
944952
953+ Using this allows code to be written in a portable fashion, so that the custom
954+ inspect function is used in an Node.js environment and ignored in the browser.
955+ The `util.inspect()` function itself is passed as third argument to the custom
956+ inspect function to allow further portability.
957+
945958```js
946- const inspect = Symbol.for('nodejs.util.inspect.custom');
959+ const customInspectSymbol = Symbol.for('nodejs.util.inspect.custom');
947960
948961class Password {
949962 constructor(value) {
@@ -954,7 +967,7 @@ class Password {
954967 return 'xxxxxxxx';
955968 }
956969
957- [inspect]( ) {
970+ [customInspectSymbol](depth, inspectOptions, inspect ) {
958971 return `Password <${this.toString()}>`;
959972 }
960973}
0 commit comments