在node.js中,
console.log调用
util.inspect每个参数时不使用格式占位符。因此,如果
inspect(depth,opts)在对象上定义方法,则将调用该方法以获取对象的自定义字符串表示形式。
例如:
function Custom(foo) { this.foo = foo;}Custom.prototype.inspect = function(depth, opts) { return 'foo = ' + this.foo.toUpperCase();};var custom = new Custom('bar');console.log(custom);输出:
foo = BAR
或使用课程:
class Custom { constructor(foo) { this.foo = foo; } inspect(depth, opts) { return 'foo = ' + this.foo.toUpperCase(); }}var custom = new Custom('bar');console.log(custom);


