diff --git a/doc/api/util.md b/doc/api/util.md index 2b1e71100503d5..8dfbfd874c762a 100644 --- a/doc/api/util.md +++ b/doc/api/util.md @@ -572,9 +572,10 @@ terminals. -Objects may also define their own `[util.inspect.custom](depth, opts)` -(or the equivalent but deprecated `inspect(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] (or the equivalent +but deprecated `inspect(depth, opts)`) function, which `util.inspect()` will +invoke and use the result of when inspecting the object: ```js const util = require('util'); @@ -626,10 +627,41 @@ util.inspect(obj); ### util.inspect.custom -A {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