使v8对象属性和方法对JS可见

我用node::ObjectWrap包装一个C ++对象,并且有一些定义如下的方法:

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

一切都按预期工作,我可以通过以下方式在JS中创build对象的实例:

 var classInstance = new className(); 

所有的方法工作得很好,但是当我尝试login函数:

 console.log(classInstance); 

我期待看到像这样的东西:

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

但是我得到的是:

 {} 

任何想法如何使这些可见(又名enumerable)?

你有什么本质上的

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

事情是打印出来的东西不包括原型链中的值。 所以inst实际上没有任何属性可以打印,因此{} 。 只有inst.__proto__具有属性。 属性是可枚举的,所以你可以做Object.keys(inst.__proto__); 看到他们,但他们不属于own属性。