Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

请问setProperty的对象怎么支持instanceof #85

Open
yangankang opened this issue Oct 22, 2024 · 5 comments
Open

请问setProperty的对象怎么支持instanceof #85

yangankang opened this issue Oct 22, 2024 · 5 comments

Comments

@yangankang
Copy link

obj instanceof OutObject

请问OutObject使用Java定义,怎么实现instanceof功能?非常感谢

@HarlonWang
Copy link
Owner

没太明白你的问题,可以举个例子吗

@yangankang
Copy link
Author

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,且它们的原型是同一个,非常感谢

@yangankang
Copy link
Author

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

@HarlonWang
Copy link
Owner

明白了,不过这个能力目前还不支持 😂

@yangankang
Copy link
Author

好的,感谢

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants