Sepehr Sepehr - 3 months ago 15
Node.js Question

Make v8 object properties and methods visible to JS

I am wrapping a C++ object with

and I have some methods defined like:

auto tpl = NanNew<v8::FunctionTemplate>(New);

NanSetPrototypeTemplate(tpl, NanNew("method1") , NanNew<v8::FunctionTemplate>(Method1) , v8::ReadOnly);
NanSetPrototypeTemplate(tpl, NanNew("method2") , NanNew<v8::FunctionTemplate>(Method2), v8::ReadOnly);
NanSetPrototypeTemplate(tpl, NanNew("method3") , NanNew<v8::FunctionTemplate>(Method3) , v8::ReadOnly);
NanSetPrototypeTemplate(tpl, NanNew("method4") , NanNew<v8::FunctionTemplate>(Method4), v8::ReadOnly);

Everything works just as expected and I can make an instance of the object in JS by:

var classInstance = new className();

All methods work just fine but when I try to log the function:


I am expecting to see something like:

method1 : [Native Function],
method2 : [Native Function],
method3 : [Native Function],
method4 : [Native Function]

But what I get is:


Any thoughts on how to make these visible (aka enumerable)?


What you have is essentially

var tpl = function(){};
tpl.prototype.method1 = function(){};
tpl.prototype.method2 = function(){};
tpl.prototype.method3 = function(){};
tpl.prototype.method4 = function(){};

var inst = new tpl();


The thing is that what is printed out does not include values in the prototype chain. So inst doesn't actually have any properties to print, hence {}. Only inst.__proto__ has properties. The properties are enumerable, so you could do Object.keys(inst.__proto__); to see them, but they aren't own properties of inst.