【问题标题】:nodejs: function template without return valuenodejs:没有返回值的函数模​​板
【发布时间】:2013-08-07 07:52:52
【问题描述】:

我按照node.js 的指示实现了工厂包装的对象。
到目前为止它有效。但是我没有开始工作的是一个没有返回值的函数。
即:(链接中示例的扩展)
myObject.h

tpl->PrototypeTemplate()->Set(String::NewSymbol("some"),
  FunctionTemplate::New(something)->GetFunction());

static void something (const v8::Arguments& args);

myObject.cc

void MyObject::something(const Arguments& args) {
  .. something without return value ...
}

不起作用。为什么?

我收到以下错误:

error: invalid conversion from ‘void (*)(const v8::Arguments&)’ to ‘v8::InvocationCallback {aka v8::Handle<v8::Value> (*)(const v8::Arguments&)}’ [-fpermissive]
error: initializing argument 1 of ‘static v8::Local<v8::FunctionTemplate> v8::FunctionTemplate::New(v8::InvocationCallback, v8::Handle<v8::Value>,  v8::Handle<v8::Signature>)’ [-fpermissive]

我真的需要返回值吗?我的意思是我可以返回 null 并忽略它,这不是问题,但这不是一个很好的解决方案。

【问题讨论】:

    标签: node.js function-templates node-gyp


    【解决方案1】:

    错误是因为FunctionTemplate::New() 需要一个InvocationCallback,它的return 类型为Handle&lt;Value&gt;

    所以,你必须return 一些东西,但它可以简单地是Undefined()

    Handle<Value> MyObject::something(const Arguments& args) {
        HandleScope scope;
        return scope.Close(Undefined());
    }
    

    这使得它等同于:

    function something() {}
    

    其中有一个隐含的return;(或return undefined;)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-10
      相关资源
      最近更新 更多