繁体   English   中英

存储JavaScript构造函数以备后用

[英]Storing a JavaScript constructor for later use

我正在编写一个由50%Pure JavaScript类和50%Pure C ++类组成的node.js(0.12)库。 C ++类中的某些函数需要返回JavaScript类的实例。 我想我需要将JavaScript类的构造函数存储在Persistent<Function> 假设我可以将构造函数作为参数,那么如何存储它们以供以后的NewInstance()处理。

JS

function MyType()
{
    this.a = 0; 
};

native.store (MyType)

C ++

void Wrapper::store (const FunctionCallbackInfo<Value>& args)
{
    // Need to store args[0] as MyTypeConstructor for later
}

void Wrapper::use (const FunctionCallbackInfo<Value>& args)
{
    Isolate* isolate = Isolate::GetCurrent();
    HandleScope scope (isolate);

    auto ctor = Local<Function>::New
        (isolate, MyTypeConstructor);

    ctor->NewInstance();
}

我想我找到了答案。 就像在MyObject::Init下的示例中一样,您可以使用Reset函数将本地函数绑定到持久性函数。

Persistent<Function> MyTypeConstructor;

void Wrapper::store (const FunctionCallbackInfo<Value>& args)
{
    Isolate* isolate = Isolate::GetCurrent();
    HandleScope scope (isolate);

    auto ctor = Local<Function>::Cast (args[0]);
    MyTypeConstructor.Reset (isolate, ctor);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM