@@ -574,9 +574,10 @@ terminals.
574
574
575
575
<!-- type=misc -->
576
576
577
- Objects may also define their own ` [util.inspect.custom](depth, opts) ` function
578
- that ` util.inspect() ` will invoke and use the result of when inspecting the
579
- object:
577
+ Objects may also define their own
578
+ [ ` [util.inspect.custom](depth, opts) ` ] [ util.inspect.custom ] function,
579
+ which ` util.inspect() ` will invoke and use the result of when inspecting
580
+ the object:
580
581
581
582
``` js
582
583
const util = require (' util' );
@@ -628,10 +629,41 @@ util.inspect(obj);
628
629
### util.inspect.custom
629
630
<!-- YAML
630
631
added: v6.6.0
632
+ changes:
633
+ - version: REPLACEME
634
+ pr-url: https://github.com/nodejs/node/pull/20857
635
+ description: This is now defined as a shared symbol.
631
636
-->
632
637
633
- * {symbol} that can be used to declare custom inspect functions, see
634
- [ Custom inspection functions on Objects] [ ] .
638
+ * {symbol} that can be used to declare custom inspect functions.
639
+
640
+ In addition to being accessible through ` util.inspect.custom ` , this
641
+ symbol is [ registered globally] [ global symbol registry ] and can be
642
+ accessed in any environment as ` Symbol.for('nodejs.util.inspect.custom') ` .
643
+
644
+ ``` js
645
+ const inspect = Symbol .for (' nodejs.util.inspect.custom' );
646
+
647
+ class Password {
648
+ constructor (value ) {
649
+ this .value = value;
650
+ }
651
+
652
+ toString () {
653
+ return ' xxxxxxxx' ;
654
+ }
655
+
656
+ [inspect ]() {
657
+ return ` Password <${ this .toString ()} >` ;
658
+ }
659
+ }
660
+
661
+ const password = new Password (' r0sebud' );
662
+ console .log (password);
663
+ // Prints Password <xxxxxxxx>
664
+ ```
665
+
666
+ See [ Custom inspection functions on Objects] [ ] for more details.
635
667
636
668
### util.inspect.defaultOptions
637
669
<!-- 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