Deno.inspect
inspect
-
inspect(value: unknown, options?: InspectOptions): string
-
不稳定:字符串输出的确切形式仍在考虑,可能会更改。
UNSTABLE*: The exact form of the string output is under consideration and may change.
将输入转换为与
console.log()
打印格式相同的字符串。Converts the input into a string that has the same format as printed by
console.log()
.const obj = {}; obj.propA = 10; obj.propB = "hello" const objAsString = Deno.inspect(obj); //{ propA: 10, propB: "hello" } console.log(obj); // 输出与 objAsString 相同的值,例如: { propA: 10, propB: "hello" }
你还可以通过对象上的
Deno.symbols.customInspect
函数 注册自定义的 inspect function,以控制和自定义输出。You can also register custom inspect functions, via the
customInspect
Deno symbol on objects, to control and customize the output.class A { x = 10; y = "hello"; [Deno.symbols.customInspect](): string { return "x=" + this.x + ", y=" + this.y; } }
const inStringFormat = Deno.inspect(new A()); //"x=10, y=hello" console.log(inStringFormat); // 输出 "x=10, y=hello"
同时还提供了一些输出选项。
Finally, a number of output options are also available.
const out = Deno.inspect(obj, {showHidden: true, depth: 4, colors: true, indentLevel: 2});
参数
- ##### value: unknown
- ##### Optional options: [InspectOptions](interfaces/deno.inspectoptions.html)