How to pass the second parameter of ObjectTemplate::New in Google V8?

65 views
Skip to first unread message

XadillaX Scarlet

unread,
May 11, 2017, 5:02:17 AM5/11/17
to v8-users

I know creating an ObjectTemplate and we can do several things to it. But my question is not about those well-known things.

I want to know how to pass the second parameter.

As the official guide said:

Each function template has an associated object template. This is used to configure objects created with this function as their constructor.

And the second parameter of ObjectTemplate::New is a constructor typed by FunctionTemplate.

static Local<ObjectTemplate> New(Isolate *isolate, Local<FunctionTemplate> constructor = Local<FunctionTemplate>());

That means something like this:

void Constructor(const FunctionCallbackInfo<Value>& args)
{
    // ...
}

Local<FunctionTemplate> _constructor = FunctionTemplate::New(isolate, Constructor);
Local<ObjectTemplate> tpl = ObjectTemplate::New(isolate, _constructor);

Who can give me a demo that how to implement the Constructor function.

I tried this, but failed:

void Constructor(const FunctionCallbackInfo<Value>& args)
{
    Isolate* isolate = args.GetIsolate();
    args.This()->Set(String::NewFromUtf8(isolate, "value"), Number::New(isolate, 233));
    args.GetReturnValue().Set(args.This());
}

By the way, I know the use case of accessors and so on, I just want to know how to use the second parameter.

Ben Noordhuis

unread,
May 12, 2017, 1:40:14 PM5/12/17
to v8-users
When you say 'failed', what exactly do you mean? Is the function not
called or don't you see the .value property on the new object?

Jakob Kummerow

unread,
May 12, 2017, 2:56:34 PM5/12/17
to v8-users


--
--
v8-users mailing list
v8-u...@googlegroups.com
http://groups.google.com/group/v8-users
---
You received this message because you are subscribed to the Google Groups "v8-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to v8-users+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages