在 GWT Javascript 覆盖类型 (JSO) 中使用 Java 常量?

发布于 2024-12-20 10:12:09 字数 1191 浏览 5 评论 0原文

我想将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

丶情人眼里出诗心の 2024-12-27 10:12:09

引用静态变量的正确语法是:

@fully.qualified.MyJSO::MY_CONST

无限定符(this.,在您的示例中)是必需的,因为该变量是静态的。

如果您想使用常量名称设置/获取 JavaScript 对象的属性,请执行以下操作:

public native void setMyProp(final boolean pFlag) /*-{
    this[@fully.qualified.MyJSO::MY_CONST] = pFlag;
}-*/;

public native boolean isMyProp() /*-{
    if (this[@fully.qualified.MyJSO::MY_CONST] != null) {
        return this[@fully.qualified.MyJSO::MY_CONST];
    } else {
        return false;
    }
}-*/;

The correct syntax to refer to a static variable is:

@fully.qualified.MyJSO::MY_CONST

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:

public native void setMyProp(final boolean pFlag) /*-{
    this[@fully.qualified.MyJSO::MY_CONST] = pFlag;
}-*/;

public native boolean isMyProp() /*-{
    if (this[@fully.qualified.MyJSO::MY_CONST] != null) {
        return this[@fully.qualified.MyJSO::MY_CONST];
    } else {
        return false;
    }
}-*/;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文