-
Notifications
You must be signed in to change notification settings - Fork 29
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
请问该库能实现把dart变量或者对象注入到JavaScript代码里? #11
Comments
flutter没法反射。 jsScript: (handler) => {
function Person() {
const opaque = handler["new"]();
Object.defineProperty(this, "name", {
get: () => handler["getName"](opaque),
set: (val) => handler["setName"](opaque, val),
});
}
this.Person = Person;
} final setupPerson = await engine.evaluate(jsScript);
await setupPerson.invoke({
"new" : () => Person(),
"getName": (person) => person.name,
"setName": (person, name) => person.name = name,
});
setupPerson.free(); 然后就可以在js里新建Person了 const psn = new Person();
psn.name = "name"; |
呃,可能我没说清楚,我的意思是把dart代码里的Person对象实例设置到JavaScript代码的全局变量,然后可以在JavaScript代码里头使用这个Person对象,访问它的属性跟方法。 |
差不多的,还是得在dart里写wrapper: jsScript: (obj, handler) => {
Object.defineProperty(obj, "name", {
get: () => handler["getName"](obj),
set: (val) => handler["setName"](obj, val),
});
this.person = obj;
} dart: final setupPerson = await engine.evaluate(jsScript);
final person = Person();
await setupPerson.invoke([
person,
{
"new" : () => Person(),
"getName": (person) => person.name,
"setName": (person, name) => person.name = name,
}
]);
setupPerson.free(); |
感觉直接给globalThis设置属性更简单,这flutter_qjs里,Dart的Map就是Js的Object。 final person = Person();
final setToGlobalObject = await engine.evaluate("(key, val) => { this[key] = val; }");
await (setToGlobalObject as JSInvokable).invoke([
"person",
{
"getName": person.getName,
"setName": person.setName
}
]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
就是有个Person dart对象 里面有age name属性,我想在JavaScript代码里面使用这个person对象。
The text was updated successfully, but these errors were encountered: