@@ -577,7 +577,7 @@ changes:
577
577
codes. Colors are customizable. See [ Customizing ` util.inspect ` colors] [ ] .
578
578
** Default:** ` false ` .
579
579
* ` customInspect ` {boolean} If ` false ` ,
580
- ` [util.inspect.custom](depth, opts) ` functions are not invoked.
580
+ ` [util.inspect.custom](depth, opts, inspect ) ` functions are not invoked.
581
581
** Default:** ` true ` .
582
582
* ` showProxy ` {boolean} If ` true ` , ` Proxy ` inspection includes
583
583
the [ ` target ` and ` handler ` ] [ ] objects. ** Default:** ` false ` .
@@ -872,10 +872,18 @@ ignored, if not supported.
872
872
873
873
<!-- type=misc -->
874
874
875
+ <!-- YAML
876
+ added: v0.1.97
877
+ changes:
878
+ - version: REPLACEME
879
+ pr-url: https://github.com/nodejs/node/pull/41019
880
+ description: The inspect argument is added for more interoperability.
881
+ -->
882
+
875
883
Objects may also define their own
876
- [ ` [util.inspect.custom](depth, opts) ` ] [ util.inspect.custom ] function,
884
+ [ ` [util.inspect.custom](depth, opts, inspect ) ` ] [ util.inspect.custom ] function,
877
885
which ` util.inspect() ` will invoke and use the result of when inspecting
878
- the object:
886
+ the object.
879
887
880
888
``` js
881
889
const util = require (' util' );
@@ -885,7 +893,7 @@ class Box {
885
893
this .value = value;
886
894
}
887
895
888
- [util .inspect .custom ](depth , options ) {
896
+ [util .inspect .custom ](depth , options , inspect ) {
889
897
if (depth < 0 ) {
890
898
return options .stylize (' [Box]' , ' special' );
891
899
}
@@ -896,8 +904,8 @@ class Box {
896
904
897
905
// Five space padding because that's the size of "Box< ".
898
906
const padding = ' ' .repeat (5 );
899
- const inner = util . inspect (this .value , newOptions)
900
- .replace (/ \n / g , ` \n ${ padding} ` );
907
+ const inner = inspect (this .value , newOptions)
908
+ .replace (/ \n / g , ` \n ${ padding} ` );
901
909
return ` ${ options .stylize (' Box' , ' special' )} < ${ inner} >` ;
902
910
}
903
911
}
@@ -908,9 +916,9 @@ util.inspect(box);
908
916
// Returns: "Box< true >"
909
917
```
910
918
911
- Custom ` [util.inspect.custom](depth, opts) ` functions typically return a string
912
- but may return a value of any type that will be formatted accordingly by
913
- ` util.inspect() ` .
919
+ Custom ` [util.inspect.custom](depth, opts, inspect ) ` functions typically return
920
+ a string but may return a value of any type that will be formatted accordingly
921
+ by ` util.inspect() ` .
914
922
915
923
``` js
916
924
const util = require (' util' );
@@ -940,8 +948,13 @@ In addition to being accessible through `util.inspect.custom`, this
940
948
symbol is [ registered globally] [ global symbol registry ] and can be
941
949
accessed in any environment as ` Symbol.for('nodejs.util.inspect.custom') ` .
942
950
951
+ Using this allows code to be written in a portable fashion, so that the custom
952
+ inspect function is used in an Node.js environment and ignored in the browser.
953
+ The ` util.inspect() ` function itself is passed as third argument to the custom
954
+ inspect function to allow further portability.
955
+
943
956
``` js
944
- const inspect = Symbol .for (' nodejs.util.inspect.custom' );
957
+ const customInspectSymbol = Symbol .for (' nodejs.util.inspect.custom' );
945
958
946
959
class Password {
947
960
constructor (value ) {
@@ -952,7 +965,7 @@ class Password {
952
965
return ' xxxxxxxx' ;
953
966
}
954
967
955
- [inspect ]( ) {
968
+ [customInspectSymbol ]( depth , inspectOptions , inspect ) {
956
969
return ` Password <${ this .toString ()} >` ;
957
970
}
958
971
}
0 commit comments