2

I am wrapping a C++ object with node::ObjectWrap and I have some methods defined like:

auto tpl = NanNew<v8::FunctionTemplate>(New);
tpl->SetClassName(NanNew("className"));
tpl->InstanceTemplate()->SetInternalFieldCount(4);

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:

console.log(classInstance);

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)?

ZachB
  • 13,051
  • 4
  • 61
  • 89
Sepehr
  • 2,051
  • 19
  • 29
  • 1
    They are enumerable, it's just that they are on `classInstance.__proto__`, not on `classInstance` directly, and the default object serialization doesn't print the prototype chain. – loganfsmyth Jul 18 '14 at 16:50
  • @loganfsmyth Could you please make this an answer? That's what I exactly was missing. – Sepehr Jul 18 '14 at 16:55

1 Answers1

2

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();

console.log(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.

loganfsmyth
  • 156,129
  • 30
  • 331
  • 251