@@ -572,9 +572,10 @@ terminals.
572
572
573
573
<!-- type=misc -->
574
574
575
- Objects may also define their own ` [util.inspect.custom](depth, opts) `
576
- (or the equivalent but deprecated ` inspect(depth, opts) ` ) function that
577
- ` util.inspect() ` will invoke and use the result of when inspecting the object:
575
+ Objects may also define their own
576
+ [ ` [util.inspect.custom](depth, opts) ` ] [ util.inspect.custom ] (or the equivalent
577
+ but deprecated ` inspect(depth, opts) ` ) function, which ` util.inspect() ` will
578
+ invoke and use the result of when inspecting the object:
578
579
579
580
``` js
580
581
const util = require (' util' );
@@ -626,10 +627,41 @@ util.inspect(obj);
626
627
### util.inspect.custom
627
628
<!-- YAML
628
629
added: v6.6.0
630
+ changes:
631
+ - version: REPLACEME
632
+ pr-url: https://github.com/nodejs/node/pull/20857
633
+ description: This is now defined as a shared symbol.
629
634
-->
630
635
631
- A {symbol} that can be used to declare custom inspect functions, see
632
- [ Custom inspection functions on Objects] [ ] .
636
+ * {symbol} that can be used to declare custom inspect functions.
637
+
638
+ In addition to being accessible through ` util.inspect.custom ` , this
639
+ symbol is [ registered globally] [ global symbol registry ] and can be
640
+ accessed in any environment as ` Symbol.for('nodejs.util.inspect.custom') ` .
641
+
642
+ ``` js
643
+ const inspect = Symbol .for (' nodejs.util.inspect.custom' );
644
+
645
+ class Password {
646
+ constructor (value ) {
647
+ this .value = value;
648
+ }
649
+
650
+ toString () {
651
+ return ' xxxxxxxx' ;
652
+ }
653
+
654
+ [inspect ]() {
655
+ return ` Password <${ this .toString ()} >` ;
656
+ }
657
+ }
658
+
659
+ const password = new Password (' r0sebud' );
660
+ console .log (password);
661
+ // Prints Password <xxxxxxxx>
662
+ ```
663
+
664
+ See [ Custom inspection functions on Objects] [ ] for more details.
633
665
634
666
### util.inspect.defaultOptions
635
667
<!-- YAML
@@ -2076,7 +2108,6 @@ Deprecated predecessor of `console.log`.
2076
2108
[ `Array.isArray()` ] : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/isArray
2077
2109
[ `ArrayBuffer` ] : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer
2078
2110
[ `ArrayBuffer.isView()` ] : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer/isView
2079
- [ async function ] : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function
2080
2111
[ `assert.deepStrictEqual()` ] : assert.html#assert_assert_deepstrictequal_actual_expected_message
2081
2112
[ `Buffer.isBuffer()` ] : buffer.html#buffer_class_method_buffer_isbuffer_obj
2082
2113
[ `console.error()` ] : console.html#console_console_error_data_args
@@ -2118,6 +2149,9 @@ Deprecated predecessor of `console.log`.
2118
2149
[ Module Namespace Object ] : https://tc39.github.io/ecma262/#sec-module-namespace-exotic-objects
2119
2150
[ WHATWG Encoding Standard ] : https://encoding.spec.whatwg.org/
2120
2151
[ Common System Errors ] : errors.html#errors_common_system_errors
2152
+ [ async function ] : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function
2121
2153
[ constructor ] : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/constructor
2154
+ [ global symbol registry ] : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol/for
2122
2155
[ list of deprecated APIS ] : deprecations.html#deprecations_list_of_deprecated_apis
2123
2156
[ semantically incompatible ] : https://github.com/nodejs/node/issues/4179
2157
+ [ util.inspect.custom ] : #util_util_inspect_custom
0 commit comments