4

Does Deno have a utility function that dumps a Object or primitive to a string like Node.js util.inspect?

For example in Node.js I create an json object and want to dump out its contents in Node:

> m = {k1:'v1', k2:'v2'}
{ k1: 'v1', k2: 'v2' }
> util.inspect(m)
'{ k1: \'v1\', k2: \'v2\' }'
4

2 回答 2

8

Deno's equivalent of Node's util.inspect is Deno.inspect.

For example in the deno REPL:

> m = {k1:'v1', k2:'v2'}
{ k1: "v1", k2: "v2" }
> Deno.inspect(m)
{ k1: "v1", k2: "v2" }
于 2020-05-11T03:49:29.217 回答
3

此外,如果您想重写以从 中返回您自己的结果,您可以向您的类Deno.inspect添加一个方法,如下所示:Deno.customInspect

class someClass {
    // code ...

    [Deno.customInspect]() {
        return 'any ol string';
    }

}

这是和的文档Deno.inspectDeno.customInspect

于 2021-01-20T01:07:23.403 回答