Node.js 我需要定义什么方法来实现nodejs/v8中的inspect函数
我有一个C++类集成到NodeJS中,我想更改它的默认对象打印 例如:Node.js 我需要定义什么方法来实现nodejs/v8中的inspect函数,node.js,v8,Node.js,V8,我有一个C++类集成到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 我已经在外部代码中定
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") );
}
void WrappedThing::ToString(const v8::FunctionCallbackInfo&args){
隔离*隔离=args.GetIsolate();
args.GetReturnValue().Set(字符串::NewFromUtf8(隔离,“欺骗一天”);
}
是否有要覆盖的“检查”方法
TIAnode.js中有一节介绍了这一点。基本上,您可以在对象/类上公开
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));
就是这样:一个名为Inspect的c++方法-与ToString()相同的原型