我用了一段时间的node.js,现在我需要写一个插件,但我是c++的新手。
在 node.js 中,我可以将可选参数传递给函数,并检查它们的类型。
function hello(arg, options, callback){
if(!callback){
if(typeof options === 'function'){
callback = options;
options = {};
}
}
console.log(typeof arg);
}
但是在插件中。
Handle<Value> hello(const Arguments &args) {
HandleScope scope;
printf("%d\n", args.Length());
// how to check type of args[i]
return String::New("world");
}
最佳答案
您应该查看 http://v8.googlecode.com/svn/trunk/include/v8.h 中的 API .您感兴趣的大多数函数都在 Value
类中。这里有在线文档,http://bespin.cz/~ondras/html/classv8_1_1Value.html但这看起来只是一些随机的人上传的文档版本。不确定他们是否在其他地方在线。
类似这样的事情应该与您的 JS 片段做同样的事情。
Handle<Value> hello(const Arguments &args) {
HandleScope scope;
Local<Value> arg(args[0]);
Local<Value> options(args[1]);
Local<Value> callback(args[2]);
if (callback.equals(False())) {
if (options->IsFunction()) {
callback = options;
options = Object::New();
}
}
// ...
}
关于c++ - node.js 插件如何检查参数类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8551665/