在 V8 Javascript 引擎中,如何使用 C++ API 添加一个 FunctionTemplate 作为另一个 FunctionTemplate 的属性?

In the V8 Javascript Engine, how do you add a FunctionTemplate as an attribute of another FunctionTemplate with the C++ API?

我有一个函数设置为 return 一个包装的 C++ 对象,当被调用时

new MyClass();

但我也想说

MyClass.do_something();

我知道如何做我想做的事情 javascript:

MyClass.prototype = { do_something: function(){}};

但我如何在 C++ 中做同样的事情?

我知道 v8::FunctionTemplate 上的 InstanceTemplate() 和 PrototypeTemplate() 方法,但这些方法似乎只用于创建新对象 returned [=15] =] 被调用。我如何获得实际函数的原型?

谢谢。

我看到了这个 post,但不确定它是否相关:Add a function template to a global object prototype in v8

原来是我想多了。

您只需在 v8::FunctionTemplate 上调用 .Set() 并传入另一个 v8::FunctionTemplate 作为值。

my_constructor_function_template.Set("static_method_name", static_method_function_template);