简体   繁体   English

将变量名称传递到GWT的JSNI中

[英]Passing variable name into GWT's JSNI

Using the JavaScript Native Interface of GWT I can perform the following: 使用GWT的JavaScript本机接口,我可以执行以下操作:

public native static String getNativeVariableFoo() /*-{
    return $wnd.foo;
}-*/;

Which will return the contents of a JavaScript variable called foo. 它将返回JavaScript变量foo的内容。

How can I expand upon this to accept the variable name as a parameter? 我该如何扩展以接受变量名作为参数? ie: 即:

public native static String getNativeVariable(String foo) /*-{
    /* Somehow meaningfully concat '$wnd.' with value of foo */
}-*/;

Simply using the variable name inside the native code like one would to call: 就像在调用本机代码中那样简单地使用变量名:

eval(foo)

results in the JavaScript hunting for a declaration of a variable named foo and not one named with the value of foo. 导致JavaScript搜索一个名为 foo的变量的声明,而不是一个以foo的命名的变量的声明。

Thanks very much! 非常感谢!

Does 是否

$wnd[foo] 

not work? 不行?

You may also want to look at the GWT 'Dictionary' class. 您可能还想看看GWT的“字典”类。 It's ideal for loading values, ie parameters from the host page. 非常适合从主机页面加载值(即参数)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM