customInspect: true,//如果为 false,则 object 上自定义的 inspect(depth, opts) 函数不会被调用。 默认为 true
showProxy: false,//如果为 true,则 Proxy 对象的对象和函数会展示它们的 target 和 handler 对象。 默认为 false
maxArrayLength: 100,//指定格式化时数组和 TypedArray 元素能包含的最大数量。 默认为 100。 设为 null 则显式全部数组元素。 设为 0 或负数则不显式数组元素。
breakLength: 60//一个对象的键被拆分成多行的长度。 设为 Infinity 则格式化一个对象为单行。 默认为 60。
};
console.log(util.inspect(util, inspectOpt));
util.inspect.styles, util.inspect.colors
说明:
可以通过 util.inspect.styles 和 util.inspect.colors 属性全局地自定义 util.inspect 的颜色输出(如果已启用)。
预定义的颜色代码有:white、grey、black、blue、cyan、green、magenta、red 和 yellow。
还有 bold、italic、underline 和 inverse 代码。
颜色样式使用 ANSI 控制码,可能不是所有终端都支持。
demo:
const util = require('util');
console.log(util.inspect.styles);
// { special: 'cyan',
// number: 'yellow',
// boolean: 'yellow',
// undefined: 'grey',
// null: 'bold',
// string: 'green',
// symbol: 'green',
// date: 'magenta',
// regexp: 'red' }
console.log(util.inspect.colors);
// { bold: [ 1, 22 ],
// italic: [ 3, 23 ],
// underline: [ 4, 24 ],
// inverse: [ 7, 27 ],
// white: [ 37, 39 ],
// grey: [ 90, 39 ],
// black: [ 30, 39 ],
// blue: [ 34, 39 ],
// cyan: [ 36, 39 ],
// green: [ 32, 39 ],
// magenta: [ 35, 39 ],
// red: [ 31, 39 ],
// yellow: [ 33, 39 ] }util.inspect.custom
说明:
util.inspect.custom是一个符号,可被用于声明自定义的查看函数:[util.inspect.custom](depth, opts)
自定义 inspect 方法的返回值可以使任何类型的值,它会被 util.inspect() 格式化。
demo:
const util = require('util');
class Box {
[util.inspect.custom](depth, options) {
return "myInspect";
}
}
const box = new Box();
console.log(util.inspect(box));
// 输出:myInspect
util.inspect.defaultOptions
说明:
defaultOptions 值允许对 util.inspect 使用的默认选项进行自定义。
它需被设为一个对象,包含一个或多个有效的 util.inspect() 选项。 也支持直接设置选项的属性。
demo:
const util = require('util');
util.inspect.defaultOptions = {
showHidden: true,
depth:3
};
util.inspect.defaultOptions.breakLength = 30;









