我需要定义什么方法来实现 nodejs/v8 中的检查功能

What method do I need to define to implement inspect function in nodejs/v8

我有一个 C++ class 集成到 NodeJS 中,我想更改它的默认对象打印

示例:

var X = new mypkg.Thing() ;
console.log( X ) ;             // prints the object in std way
console.log( X.toString() ) ;  // prints Diddle aye day
console.log( '' + X ) ;        // prints Diddle aye day

我已经在外部代码中定义了 ToString,可以。但我希望默认打印相同。

 void WrappedThing::ToString( const v8::FunctionCallbackInfo<v8::Value>& args ) {
     Isolate* isolate = args.GetIsolate();
     args.GetReturnValue().Set( String::NewFromUtf8( isolate, "Diddle aye day") );
 }

是否有 'Inspect' 方法可以覆盖?

TIA

在 node.js util documentation 中有一节是关于这个的。基本上,您可以在 object/class 上公开一个 inspect() 方法,或者 通过对象上的特殊 util.inspect.custom 符号设置函数。

这是一个使用特殊符号的例子:

const util = require('util');

const obj = { foo: 'this will not show up in the inspect() output' };
obj[util.inspect.custom] = function(depth, options) {
  return { bar: 'baz' };
};

// Prints: "{ bar: 'baz' }"
console.log(util.inspect(obj));