3

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

Community
  • 1
  • 1
xaxxon
  • 19,189
  • 5
  • 50
  • 80
  • See http://stackoverflow.com/questions/36491385/how-is-asynchronous-javascript-interpreted-and-executed-in-node-js – guest271314 Jun 21 '16 at 01:30
  • I don't see anything in there that's specifically answering my question. All I see is "use templates" which I already am: "I'm aware of the InstanceTemplate() and PrototypeTemplate() methods on v8::FunctionTemplate" – xaxxon Jun 21 '16 at 01:41
  • 1
    I think I was way overthinking this. You just call .Set on the function template. – xaxxon Jun 21 '16 at 02:24
  • See http://stackoverflow.com/help/self-answer – guest271314 Jun 21 '16 at 02:33
  • 1
    yeah, that was more of a guess before. But now that I've confirmed, I've self answered. I seem to do that a lot in the [v8] tag. Now I just need to remember to come back and accept the answer in 2 days. – xaxxon Jun 21 '16 at 02:36

1 Answers1

2

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);
xaxxon
  • 19,189
  • 5
  • 50
  • 80