1

对不起,我的英语很差,希望有人能理解我所说的这样的代码

 class Point
 {
 public:
int x_, y_;
Point(int x, int y):x_(x), y_(y){}
void Test(Local<Object> & obj)
{
    //Local<Value> value = ref->Get(String::New("onconnect"));

    printf("%d", ref->Get(String::New("getX")));
}
 };


 Handle<Value> PointConstructor(const Arguments& args)
 { 

     Point *p = new Point(x, y);

     object->SetInternalField(0, External::New(p));

     p->Test();
     return object; 
 } 

 ...

 Handle<String> script = String::New("var p = new Point(20, 0); p.onconnect = function(){}; log(p.getX())");


   Handle<Script> compiled_script = Script::Compile(script);

方法“Test”mybe 是一个异步调用,在 anthor 线程中运行,当它运行完成时我调用 js 方法“onconnect”我如何调用 js 方法:在方法 Test() 中的 onconnect?

4

1 回答 1

1

我是凭记忆写的,某些类型或调用可能略有不同。假设 Test 接收到 Point 参考:

void Test(Local<Object> obj)
{
   String funName = String::New("onconnect");
   if (obj->Has(funName)){
     Local<Value> fun = obj->Get(funName);
     // would suggest to check additionally whether it actually is a function
     Local<Value> args[] = {}; // empty arguments
     Local<Value> result = fun->AsObject()->CallAsFunction(obj, 0, args);
   }
}
于 2012-11-05T23:02:50.593 回答