使用 GWT 的 JavaScript 本机接口,我可以执行以下操作:
public native static String getNativeVariableFoo() /*-{
return $wnd.foo;
}-*/;
它将返回一个名为 foo 的 JavaScript 变量的内容。
我如何扩展它以接受变量名作为参数?IE:
public native static String getNativeVariable(String foo) /*-{
/* Somehow meaningfully concat '$wnd.' with value of foo */
}-*/;
只需在本机代码中使用变量名称,就像调用一样:
eval(foo)
导致 JavaScript 寻找一个名为foo 的变量的声明,而不是一个以 foo 的值命名的变量。
非常感谢!