在nodejs中console.log一个class实例对象时,对象中的Proxy出现了额外的get访问
发布于 5 年前 作者 dislido 2913 次浏览 来自 问答
class Test {
    constructor() {
        this.test = new Proxy({ foo() {} }, {
            get(target, name) {
                console.log('name: ', name);
            },
        });
    }
}

const test = new Test();

console.log(test);
/*
name:  Symbol(nodejs.util.inspect.custom)
name:  Symbol(Symbol.toStringTag)
name:  Symbol(Symbol.iterator)
Test { test: { foo: [Function: foo] } }
*/

在浏览器中运行以上代码不会出现这些额外的get访问
为什么会出现这种情况?有没有办法避免? 环境:Nodejs v11.14.0 win10 x64 1809

1 回复

找到原因了。。。

console.log(test) - constructor.js
- formatWithOptions(inspectOptions, test) - inspect.js
- inspect(test, inspectOptions) - inspect.js
- formatValue(ctx, value <- test, 0) - inspect.js
     const maybeCustom = value[customInspectSymbol];
回到顶部