gpt4 book ai didi

java - 如何将匿名 JavaScript 对象从 Java 传递到 GWT 中的 JavaScript?

转载 作者:行者123 更新时间:2023-12-02 08:36:48 26 4
gpt4 key购买 nike

我正在围绕 JavaScript 库创建一个 GWT 包装器。 JavaScript 函数之一采用匿名对象作为其参数,例如:

obj.buildTabs({ hide: true, placeholder: 'placeholder' });

在 Java 方面,如何创建这种类型的 JavaScript 对象并将其传递给我的 native 实现?

目前,在 Java 方面我有:

public void buildTabs(TabConfiguration config) {
// ?
}

private native void buildTabs(?) /*-{
$wnd.NAMESPACE.lib.buildTabs(?);
}-*/;

感谢任何指点,谢谢。

最佳答案

如果您确切知道应该使用哪些参数,您可以执行以下操作(删除::之后的其他新行)

private native void buildTabs(TabConfiguration config) /*-{
$wnd.NAMESPACE.lib.buildTabs({hide:
config.@com.yournamehere.TabConfiguration::
getHide()(),
placeholder:
config.@com.yournamehere.TabConfiguration::
getPlaceholder()()});
}-*/;

来自GWT documentation的小剪辑:

public native void bar(JSNIExample x, String s) /*-{
// Call instance method instanceFoo() on this
this.@com.google.gwt.examples.JSNIExample::instanceFoo(Ljava/lang/String;)(s);

// Call instance method instanceFoo() on x
x.@com.google.gwt.examples.JSNIExample::instanceFoo(Ljava/lang/String;)(s);

// Call static method staticFoo()
@com.google.gwt.examples.JSNIExample::staticFoo(Ljava/lang/String;)(s);

// Read instance field on this
var val = this.@com.google.gwt.examples.JSNIExample::myInstanceField;

// Write instance field on x
x.@com.google.gwt.examples.JSNIExample::myInstanceField = val + " and stuff";

// Read static field (no qualifier)
@com.google.gwt.examples.JSNIExample::myStaticField = val + " and stuff";
}-*/;

关于java - 如何将匿名 JavaScript 对象从 Java 传递到 GWT 中的 JavaScript?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1457417/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com