首页 > 解决方案 > util.inspect 默认深度应该是 20,但看起来更小

问题描述

代码

考虑以下 Node.js 代码:

const { inspect } = require('util');

const obj = { response: [ { webhooks: [ 1, 2, 3 ] } ] };

console.log(`Obj is ${obj}`);
console.log(`Obj is ${inspect(obj, { breakLength: Infinity })}`);
console.log(`Obj is ${inspect(obj, { breakLength: Infinity, depth: 20 })}`);
console.log(`Obj is ${inspect(obj, { breakLength: Infinity, depth: Infinity })}`);

给出:

Obj is [object Object]
Obj is { response: [ { webhooks: [Array] } ] }
Obj is { response: [ { webhooks: [ 1, 2, 3 ] } ] }
Obj is { response: [ { webhooks: [ 1, 2, 3 ] } ] }

问题

根据手册

depth 指定在格式化对象时递归的次数。这对于检查大型复杂对象很有用。要使其递归到最大调用堆栈大小,请传递 Infinity 或 null。默认值:20。

据我了解,第二console.log行应该打印与第三行相同的字符串,因为默认为depth20。唉,它打印[Array]而不是[ 1, 2, 3 ].

我的问题

为什么默认调用的行为不像depth: 20

标签: node.js

解决方案


傻我。

在最新版本中,默认值从2更改20。我正在阅读 v11 的文档并使用了 v8。


推荐阅读