在nodejs/v8中实现inspect功能需要定义什么方法

Ric*_*ard 2 v8 node.js

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

例子:

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
Run Code Online (Sandbox Code Playgroud)

我在外部代码中定义了 ToString ,它有效。但我希望默认打印是相同的。

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

是否有“检查”方法可以覆盖?

TIA

msc*_*dex 5

node.jsutil文档中有一个关于此的部分。基本上,您可以公开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));
Run Code Online (Sandbox Code Playgroud)