diff --git a/doc/api/util.md b/doc/api/util.md index 44495e977d13c3..b8a54f6a97a4e1 100644 --- a/doc/api/util.md +++ b/doc/api/util.md @@ -569,9 +569,10 @@ terminals. -Objects may also define their own `[util.inspect.custom](depth, opts)` function -that `util.inspect()` will invoke and use the result of when inspecting the -object: +Objects may also define their own +[`[util.inspect.custom](depth, opts)`][util.inspect.custom] function, +which `util.inspect()` will invoke and use the result of when inspecting +the object: ```js const util = require('util'); @@ -623,10 +624,41 @@ util.inspect(obj); ### util.inspect.custom -* {symbol} that can be used to declare custom inspect functions, see -[Custom inspection functions on Objects][]. +* {symbol} that can be used to declare custom inspect functions. + +In addition to being accessible through `util.inspect.custom`, this +symbol is [registered globally][global symbol registry] and can be +accessed in any environment as `Symbol.for('nodejs.util.inspect.custom')`. + +```js +const inspect = Symbol.for('nodejs.util.inspect.custom'); + +class Password { + constructor(value) { + this.value = value; + } + + toString() { + return 'xxxxxxxx'; + } + + [inspect]() { + return `Password <${this.toString()}>`; + } +} + +const password = new Password('r0sebud'); +console.log(password); +// Prints Password +``` + +See [Custom inspection functions on Objects][] for more details. ### util.inspect.defaultOptions