我需要定义什么方法来实现nodejs / v8中的检查function
我有一个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
我已经在外部代码中定义了ToString,这是有效的。 但是我想要默认打印是一样的。
void WrappedThing::ToString( const v8::FunctionCallbackInfo<v8::Value>& args ) { Isolate* isolate = args.GetIsolate(); args.GetReturnValue().Set( String::NewFromUtf8( isolate, "Diddle aye day") ); }
是否有一个“检查”方法来重写?
TIA
在node.js util
文档中有关于此的一节。 基本上,你可以在对象/类上公开一个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));