在 GWT Javascript 覆盖类型 (JSO) 中使用 Java 常量?
我想将 GWT JSO 属性名称定义为常量JSO,为了避免拼写错误并受益于 Eclipse 代码完成,如下所示:
public final class MyJSO extends JavaScriptObject
{
/** here is the constant */
private static final String MY_CONST = "myPropName";
protected MyJSO() {
super();
}
public native void setMyProp(final boolean pFlag)
/*-{
[email protected]::MY_CONST = pFlag;
}-*/;
public native boolean isMyProp()
/*-{
if (this.hasOwnProperty(@fully.qualified.MyJSO::MY_CONST)) {
return [email protected]::MY_CONST;
} else {
return false;
}
}-*/;
}
GWT 编译器应该能够在编译时替换常量中的 String,因此以后作为 Javascript 存在的对象不会有问题。
但这完全不起作用,我想我可能是错的。 :-) 谁能解释一下为什么?您有更好的想法如何实现这一目标吗?
谢谢!
I would like to define the GWT JSO property name as a constant in the JSO, in order to avoid typos and benefit from Eclipse code completion, like so:
public final class MyJSO extends JavaScriptObject
{
/** here is the constant */
private static final String MY_CONST = "myPropName";
protected MyJSO() {
super();
}
public native void setMyProp(final boolean pFlag)
/*-{
[email protected]::MY_CONST = pFlag;
}-*/;
public native boolean isMyProp()
/*-{
if (this.hasOwnProperty(@fully.qualified.MyJSO::MY_CONST)) {
return [email protected]::MY_CONST;
} else {
return false;
}
}-*/;
}
The GWT compiler should be able to replace the String from the constant at compile time, so there is no problem with the object living as Javascript later on.
But this is so totally not working, I'm thinking I may be wrong. :-) Can anyone explain why? Do you have better ideas how to achieve this?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
引用静态变量的正确语法是:
无限定符(
this.
,在您的示例中)是必需的,因为该变量是静态的。如果您想使用常量名称设置/获取 JavaScript 对象的属性,请执行以下操作:
The correct syntax to refer to a static variable is:
No qualifier (
this.
, in your example) is needed since the variable is static.If you want to set/get a property on the JavaScript object with the constant name do so as follows: