如何在Nodejs调试控制台视图中更改对象的字符串表示形式

时间:2018-03-19 14:44:11

标签: javascript node.js debugging visual-studio-code vscode-debugger

如何在nodejs调试控制台中更改对象实例的字符串表示形式。是否有一种方法(如.NET中的.executeMedia())我可以覆盖?

enter image description here

请考虑以下代码:

toString()

这会产生以下结果: enter image description here

然而,在我使用的其他环境和编程语言中,覆盖class SomeObject{ constructor(){ this._varA = "some text"; this._varB = 12345; this._varC = "some more text"; this._varD = true; this._varE = 0.45; } toString(){ return "custom textual rapresentation of my object"; } } var array = []; array.push(new SomeObject()); array.push(new SomeObject()); array.push(new SomeObject()); console.log(array); 方法会显示toString()的结果(在上面的示例中为toString()),而不是动态文本表示由调试器创建(在上面的示例代码中是:"custom textual representation of my object") - 我不怀疑一分钟,当没有定义自定义替代时,它非常有用。

我也意识到SomeObject {_varA: "some text", _varB: 12345, _varC: "some more text", …}甚至console.log(array.toString());会产生类似于我所追求的东西,但这会阻止我使用调试导航来浏览对象,即。深入到对象图中。

如果不可能,其他人会从中受益吗?如果有足够的兴趣,我可以考虑将其定义并实现为一个功能。

5 个答案:

答案 0 :(得分:7)

当您执行console.log时,formatValue会在util.js内部调用const maybeCustomInspect = value[customInspectSymbol] || value.inspect; 进行以下检查

inspect

这意味着如果您的值有toString方法,则会调用它,然后您可以返回class SomeObject{ constructor(){ this._varA = "some text"; this._varB = 12345; this._varC = "some more text"; this._varD = true; this._varE = 0.45; } inspect(depth, opts) { return this.toString(); } toString(){ return "custom textual rapresentation of my object"; } } var array = []; array.push(new SomeObject()); array.push(new SomeObject()); array.push(new SomeObject()); console.log(array); 。所以将代码更改为

[ custom textual rapresentation of my object,
  custom textual rapresentation of my object,
  custom textual rapresentation of my object ]

打印

inspect

Nodejs也有相同的文档

https://nodejs.org/dist/latest-v8.x/docs/api/util.html#util_custom_inspection_functions_on_objects

我使用的const util = require('util'); class SomeObject{ constructor(){ this._varA = "some text"; this._varB = 12345; this._varC = "some more text"; this._varD = true; this._varE = 0.45; } [util.inspect.custom](depth, options) { return this.toString(); } toString(){ return "custom textual rapresentation of my object"; } } var array = []; array.push(new SomeObject()); array.push(new SomeObject()); array.push(new SomeObject()); console.log(array); 方法已根据文档弃用,正确的方法如下所示

$ node --inspect-brk=54223 test.js 
Debugger listening on ws://127.0.0.1:54223/81094440-716b-42a5-895e-4ea2008e0dff
For help see https://nodejs.org/en/docs/inspector

编辑:2018年3月28日

所以我使用下面的

启动了脚本
socat

然后使用下面的

运行$ socat -v TCP-LISTEN:54222,fork TCP:127.0.0.1:54223 转发器
array

在调试器中调试变量socat时,在v8终端

上获得低于输出的值

socat

信息是由调试器重建的,为您提供有意义的表示。

现在console.log调试api不知道我们想要以不同方式表示它,就像我们为V8所做的那样。现在可能在startActivityForResult(new Intent(currentActivity.this, nextActivity.class), 101); 代码中有类似的东西做类似的东西,但是看一下源代码,我无法弄清楚是否存在这样的东西。所以你可能需要向拥有V8调试器api知识的人确认,如果存在这种类型的东西

如果不是,你需要在IDE级别构建一些东西,这也不是一件容易的事情

答案 1 :(得分:1)

VS Code中添加了一个新选项,用于调整调试器的输出:只需将以下内容添加到启动配置中

"customDescriptionGenerator": "function (def) { if (this.toString) { const _v = this.toString(); if (_v.indexOf(\"[object Object]\") < 0) return _v; } return def; }",

Viola:您的实体在手表中显示为“ toString”,保留了向下钻取等功能的功能。

答案 2 :(得分:0)

我的两分钱: 如何覆盖console.log函数来执行您想要的操作。 这是POC,它需要在任何对象中使用_toString函数来改变它在日志中的显示方式。

使用以下内容创建一个logger.js文件:

const getUpdatedLogObj = function(x){


    if(x && typeof x == 'object'){
            if(typeof x._toString === 'function'){
                return x._toString()
            } else {
                for(let i in x){
                    x[i] = getUpdatedLogObj(x[i])
                }
            }
        }
        return x;
    }



    console._log = console.log
    console.log = function(x){console._log(getUpdatedLogObj({...x}))}

将其导入index.js

require('./logger')

console.log({a: 1, b: 2, c: {d: 5, e: 6, _toString: function(){return 'fromToString'}}})

你仍然得到了导航: enter image description here

答案 3 :(得分:-1)

const util = require('util');
class SomeObject{
constructor(){
    this._varA = "some text";
    this._varB = 12345;
    this._varC = "some more text";
    this._varD = true;
    this._varE = 0.45;
}

[util.inspect.custom](depth, options) {
    return this.toString();
}

toString(){
    return "custom textual rapresentation of my object";
}
}

var array = [];

array.push(new SomeObject());
array.push(new SomeObject());
array.push(new SomeObject());

console.log(array);

答案 4 :(得分:-2)

您可以在另一个字符串上调用toString()方法。

terms[200]._text.toString()

您可能也在寻找我发现在调试中非常有用的JSON.stringify()。由于JavaScript对象实际上是JSON,因此可以更简单地将它们打印到控制台。

console.log(JSON.stringify(terms[200]))