@@ -576,7 +576,7 @@ changes:
576
576
codes. Colors are customizable. See [ Customizing ` util.inspect ` colors] [ ] .
577
577
** Default:** ` false ` .
578
578
* ` customInspect ` {boolean} If ` false ` ,
579
- ` [util.inspect.custom](depth, opts) ` functions are not invoked.
579
+ ` [util.inspect.custom](depth, opts, inspect ) ` functions are not invoked.
580
580
** Default:** ` true ` .
581
581
* ` showProxy ` {boolean} If ` true ` , ` Proxy ` inspection includes
582
582
the [ ` target ` and ` handler ` ] [ ] objects. ** Default:** ` false ` .
@@ -854,9 +854,9 @@ ignored, if not supported.
854
854
<!-- type=misc -->
855
855
856
856
Objects may also define their own
857
- [ ` [util.inspect.custom](depth, opts) ` ] [ util.inspect.custom ] function,
857
+ [ ` [util.inspect.custom](depth, opts, inspect ) ` ] [ util.inspect.custom ] function,
858
858
which ` util.inspect() ` will invoke and use the result of when inspecting
859
- the object:
859
+ the object.
860
860
861
861
``` js
862
862
const util = require (' util' );
@@ -866,7 +866,7 @@ class Box {
866
866
this .value = value;
867
867
}
868
868
869
- [util .inspect .custom ](depth , options ) {
869
+ [util .inspect .custom ](depth , options , inspect ) {
870
870
if (depth < 0 ) {
871
871
return options .stylize (' [Box]' , ' special' );
872
872
}
@@ -877,8 +877,8 @@ class Box {
877
877
878
878
// Five space padding because that's the size of "Box< ".
879
879
const padding = ' ' .repeat (5 );
880
- const inner = util . inspect (this .value , newOptions)
881
- .replace (/ \n / g , ` \n ${ padding} ` );
880
+ const inner = inspect (this .value , newOptions)
881
+ .replace (/ \n / g , ` \n ${ padding} ` );
882
882
return ` ${ options .stylize (' Box' , ' special' )} < ${ inner} >` ;
883
883
}
884
884
}
@@ -889,9 +889,9 @@ util.inspect(box);
889
889
// Returns: "Box< true >"
890
890
```
891
891
892
- Custom ` [util.inspect.custom](depth, opts) ` functions typically return a string
893
- but may return a value of any type that will be formatted accordingly by
894
- ` util.inspect() ` .
892
+ Custom ` [util.inspect.custom](depth, opts, inspect ) ` functions typically return
893
+ a string but may return a value of any type that will be formatted accordingly
894
+ by ` util.inspect() ` .
895
895
896
896
``` js
897
897
const util = require (' util' );
@@ -921,8 +921,13 @@ In addition to being accessible through `util.inspect.custom`, this
921
921
symbol is [ registered globally] [ global symbol registry ] and can be
922
922
accessed in any environment as ` Symbol.for('nodejs.util.inspect.custom') ` .
923
923
924
+ Using this allows code to be written in a portable fashion, so that the custom
925
+ inspect function is used in an Node.js environment and ignored in the browser.
926
+ The ` util.inspect() ` function itself is passed as third argument to the custom
927
+ inspect function to allow further portability.
928
+
924
929
``` js
925
- const inspect = Symbol .for (' nodejs.util.inspect.custom' );
930
+ const customInspectSymbol = Symbol .for (' nodejs.util.inspect.custom' );
926
931
927
932
class Password {
928
933
constructor (value ) {
@@ -933,7 +938,7 @@ class Password {
933
938
return ' xxxxxxxx' ;
934
939
}
935
940
936
- [inspect ]( ) {
941
+ [customInspectSymbol ]( depth , inspectOptions , inspect ) {
937
942
return ` Password <${ this .toString ()} >` ;
938
943
}
939
944
}
0 commit comments