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

空扰寡人 提交于 2019-12-10 10:47:34

问题


I have a function set up to return a wrapped C++ object when called as

new MyClass();

but I want to also be able to say

MyClass.do_something();

I know how to do what I want in pure javascript:

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

but how do I do the same in C++?

I'm aware of the InstanceTemplate() and PrototypeTemplate() methods on v8::FunctionTemplate, but those seem to only be used in the creation of the new object returned when new MyClass() is called. How do I get at the actual function's prototype?

Thank you.

I saw this post, but am not sure if it's relevant: Add a function template to a global object prototype in v8


回答1:


Turns out I was way overthinking the problem.

You simply call .Set() on your v8::FunctionTemplate and pass in another v8::FunctionTemplate as the value.

my_constructor_function_template.Set("static_method_name", static_method_function_template);


来源:https://stackoverflow.com/questions/37934181/in-the-v8-javascript-engine-how-do-you-add-a-functiontemplate-as-an-attribute-o

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!