C++ 使v8对象属性和方法对JS可见
我用C++ >对象::ObjeTWrrp< /Cord>包装C++对象,我有一些定义如下的方法:C++ 使v8对象属性和方法对JS可见,c++,node.js,v8,node.js-addon,node.js-nan,C++,Node.js,V8,Node.js Addon,Node.js Nan,我用C++ >对象::ObjeTWrrp< /Cord>包装C++对象,我有一些定义如下的方法: auto tpl = NanNew<v8::FunctionTemplate>(New); tpl->SetClassName(NanNew("className")); tpl->InstanceTemplate()->SetInternalFieldCount(4); NanSetPrototypeTemplate(tpl, NanNew("method1")
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);
所有方法都可以正常工作,但当我尝试记录函数时:
console.log(classInstance);
我希望看到类似以下的情况:
{
method1 : [Native Function],
method2 : [Native Function],
method3 : [Native Function],
method4 : [Native Function]
}
但我得到的是:
{}
关于如何使这些内容可见(即可枚举)有什么想法吗?您所拥有的基本上是
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.\uuuu proto\uuu
具有属性。属性是可枚举的,因此您可以执行Object.keys(inst.\uuu proto\uu)
可以看到它们,但它们不是拥有的inst
属性,它们是可枚举的,只是它们在classInstance上。proto\uuuu
,而不是直接在classInstance上,默认的对象序列化不会打印原型链。@loganfsmyth您能回答这个问题吗?这正是我所缺少的。
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);