quickjs-wrapper
quickjs-wrapper copied to clipboard
请问setProperty的对象怎么支持instanceof
obj instanceof OutObject
请问OutObject使用Java定义,怎么实现instanceof功能?非常感谢
没太明白你的问题,可以举个例子吗
QuickJSLoader.init();
QuickJSContext context = QuickJSContext.create();
context.setConsole(new JSConsole(false));
JSObject global = context.getGlobalObject();
// 这里如何定义一个js类
global.setProperty("MyClass", MyClass.class);
global.setProperty("ins", new MyClass());
context.evaluate("""
let mc = new MyClass();
if (ins instanceof MyClass) {
console.log("success")
}
""");
context.destroy();
就是类似这种,我可以在Java定义一个类,这个类可以在Java里new,也可以在js中new,且它们的原型是同一个,非常感谢
QuickJSContext context = QuickJSContext.create();
context.setConsole(new JSConsole(false));
JSObject globalObj = context.getGlobalObject();
var prototype = context.createNewJSObject();
var object = context.createNewJSObject();
object.setProperty("__proto__", prototype);
globalObj.setProperty("obj", object);
var clazz = context.createNewJSObject();
clazz.setProperty("prototype", prototype);
globalObj.setProperty("ObjClass", clazz);
context.evaluate("""
if(obj instanceof ObjClass){
console.log('yes')
}
""");
context.destroy();
如果我使用相同原型链对象的话,会报错 com.whl.quickjs.wrapper.QuickJSException: invalid 'instanceof' right operand
明白了,不过这个能力目前还不支持 :joy:
好的,感谢