Skip to content

Commit 97f7590

Browse files
TrottMylesBorins
authored andcommitted
util: deprecate obj.inspect for custom inspection
The existence of `obj.inspect()` for custom inspection can cause people to unintentionally break `console.log()` and friends. This is a documentation-only deprecation that can hopefully land in 8.x. PR-URL: #15631 Refs: #15549 Reviewed-By: Vse Mozhet Byt <[email protected]> Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Yuta Hiroto <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: Refael Ackermann <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent da7c87f commit 97f7590

File tree

2 files changed

+15
-18
lines changed

2 files changed

+15
-18
lines changed

doc/api/deprecations.md

+12
Original file line numberDiff line numberDiff line change
@@ -653,6 +653,16 @@ difference is that `querystring.parse()` does url decoding:
653653
{ '%E5%A5%BD': '1' }
654654
```
655655
656+
<a id="DEP0079"></a>
657+
### DEP0079: Custom inspection function on Objects via .inspect()
658+
659+
Type: Documentation-only
660+
661+
Using a property named `inspect` on an object to specify a custom inspection
662+
function for [`util.inspect()`][] is deprecated. Use [`util.inspect.custom`][]
663+
instead. For backwards compatibility with Node.js prior to version 6.4.0, both
664+
may be specified.
665+
656666
[`Buffer.allocUnsafeSlow(size)`]: buffer.html#buffer_class_method_buffer_allocunsafeslow_size
657667
[`Buffer.from(array)`]: buffer.html#buffer_class_method_buffer_from_array
658668
[`Buffer.from(buffer)`]: buffer.html#buffer_class_method_buffer_from_buffer
@@ -692,6 +702,8 @@ difference is that `querystring.parse()` does url decoding:
692702
[`util._extend()`]: util.html#util_util_extend_target_source
693703
[`util.debug()`]: util.html#util_util_debug_string
694704
[`util.error()`]: util.html#util_util_error_strings
705+
[`util.inspect()`]: util.html#util_util_inspect_object_options
706+
[`util.inspect.custom`]: util.html#util_util_inspect_custom
695707
[`util.isArray()`]: util.html#util_util_isarray_object
696708
[`util.isBoolean()`]: util.html#util_util_isboolean_object
697709
[`util.isBuffer()`]: util.html#util_util_isbuffer_object

doc/api/util.md

+3-18
Original file line numberDiff line numberDiff line change
@@ -377,8 +377,8 @@ terminals.
377377
<!-- type=misc -->
378378

379379
Objects may also define their own `[util.inspect.custom](depth, opts)`
380-
(or, equivalently `inspect(depth, opts)`) function that `util.inspect()` will
381-
invoke and use the result of when inspecting the object:
380+
(or the equivalent but deprecated `inspect(depth, opts)`) function that
381+
`util.inspect()` will invoke and use the result of when inspecting the object:
382382

383383
```js
384384
const util = require('util');
@@ -388,7 +388,7 @@ class Box {
388388
this.value = value;
389389
}
390390

391-
inspect(depth, options) {
391+
[util.inspect.custom](depth, options) {
392392
if (depth < 0) {
393393
return options.stylize('[Box]', 'special');
394394
}
@@ -427,21 +427,6 @@ util.inspect(obj);
427427
// Returns: "{ bar: 'baz' }"
428428
```
429429

430-
A custom inspection method can alternatively be provided by exposing
431-
an `inspect(depth, opts)` method on the object:
432-
433-
```js
434-
const util = require('util');
435-
436-
const obj = { foo: 'this will not show up in the inspect() output' };
437-
obj.inspect = function(depth) {
438-
return { bar: 'baz' };
439-
};
440-
441-
util.inspect(obj);
442-
// Returns: "{ bar: 'baz' }"
443-
```
444-
445430
### util.inspect.custom
446431
<!-- YAML
447432
added: v6.6.0

0 commit comments

Comments
 (0)