Deno.inspect

\color{red}{function} Deno.inspect(value: unknow, options?: InspectOptions) : string

\color{red}{作用}

将输入转换为与 console.log() 打印的格式相同的字符串。

Converts the input into a string that has the same format as printed by console.log().

\color{red}{参数}

value : unknow
options?: InspectOptions

value : unknow
options?: InspectOptions

\color{red}{返回值}

string

string

\color{red}{示列代码}

const obj = {};
obj.propA = 10;
obj.propB = "hello";
const objAsString = Deno.inspect(obj); // { propA: 10, propB: "hello" }
console.log(obj);  // prints same value as objAsString, e.g. { propA: 10, propB: "hello" }

\color{red}{其它}

您还可以通过对象上的customInspect Deno符号注册自定义检查功能,以控制和自定义输出。

class A {
  x = 10;
  y = "hello";
  [Deno.customInspect](): string {
    return "x=" + this.x + ", y=" + this.y;
  }
}
const inStringFormat = Deno.inspect(new A()); // "x=10, y=hello"
 console.log(inStringFormat);  // prints "x=10, y=hello"

最后,您还可以指定其格式化的深度。

Deno.inspect({a: {b: {c: {d: 'hello'}}}}, {depth: 2}); // { a: { b: [Object] } }

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.customInspect](): string {
    return "x=" + this.x + ", y=" + this.y;
  }
}
const inStringFormat = Deno.inspect(new A()); // "x=10, y=hello"
 console.log(inStringFormat);  // prints "x=10, y=hello"

Finally, you can also specify the depth to which it will format.

Deno.inspect({a: {b: {c: {d: 'hello'}}}}, {depth: 2}); // { a: { b: [Object] } }

原文地址:点此跳转

Deno 目录

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容