如何在 javascriptcore for android 中使用 Object.defineProperty



我正在使用这个库在安卓上运行一些javascript - https://github.com/LiquidPlayer/LiquidCore/wiki/LiquidCore-as-a-Native-Javascript-Engine

我有一些对象没有问题,但我想将一些函数绑定到该类作为真正的getter/setter属性。

在javascript中执行此操作的语法是:

Object.defineProperty(viewWrapper, 'width', {
get: function () {
return viewWrapper.view.width();
}
});

我找到了这个类:http://ericwlange.github.io/org/liquidplayer/webkit/javascriptcore/JSObjectPropertiesMap.html

我在苹果文档中看到了这个参考: https://developer.apple.com/documentation/javascriptcore/jsvalue/1451542-defineproperty

我这样做的原因是完美地阴影现有对象,所以我必须能够复制 getter/setter 样式。我可以在javascript层完成工作,但我正在尝试编写尽可能少的代码,并从java端公开完全成型的对象。

我在此页面上尝试了这个,但它最终绑定了函数本身。

https://github.com/ericwlange/AndroidJSCore/issues/20

还有另一种方法可以做到这一点,它没有特别好的文档,但要优雅得多。 您可以在JSObject上使用@jsexport属性。

private class Foo extends JSObject {
Foo(JSContext ctx) { super(ctx); }
@jsexport(type = Integer.class)
Property<Integer> x;
@jsexport(type = String.class)
Property<String>  y;
@jsexport(attributes = JSPropertyAttributeReadOnly)
Property<String> read_only;
@SuppressWarnings("unused")
@jsexport(attributes = JSPropertyAttributeReadOnly | JSPropertyAttributeDontDelete)
int incr(int x) {
return x+1;
}
}

然后你可以在Java和Javascript中使用getter/setter方法:

Foo foo = new Foo(ctx);
ctx.property("foo", foo);
ctx.evaluateScript("foo.x = 5; foo.y = 'test';");
assertEquals((Integer)5, foo.x.get());
assertEquals("test", foo.y.get());
foo.x.set(6);
foo.y.set("test2");
assertEquals(6, foo.property("x").toNumber().intValue());
assertEquals("test2", foo.property("y").toString());
assertEquals(6, ctx.evaluateScript("foo.x").toNumber().intValue());
assertEquals("test2", 
ctx.evaluateScript("foo.y").toString());
ctx.evaluateScript("foo.x = 11");
assertEquals((Integer)11, foo.x.get());
assertEquals(21, 
ctx.evaluateScript("foo.incr(20)").toNumber().intValue());
foo.read_only.set("Ok!");
assertEquals("Ok!", foo.read_only.get());
foo.read_only.set("Not Ok!");
assertEquals("Ok!", foo.read_only.get());
ctx.evaluateScript("foo.read_only = 'boo';");
assertEquals("Ok!", foo.read_only.get());

最新更新