闭包编译器对命名空间枚举发出警告

发布于 2024-12-16 16:49:43 字数 431 浏览 4 评论 0原文

以下示例代码生成有关高级优化的编译器警告:“JSC_UNSAFE_NAMESPACE:为命名空间 NS 创建的不完整别名”。如果我删除 @enum 注释,它不会发出警告。

var NS = {};

/**
 * @enum {string}
 */
NS.type = {
    FOO : 'bar'
};

NS.foobar = function(){ alert(NS.type.FOO); };

window['NS'] = NS;
window['NS']['foobar'] = NS.foobar;

仅导出函数而不导出命名空间似乎也有效:

window['NS_foobar'] = NS.foobar;

我做错了什么?有办法解决这个问题吗?如果可能的话,我宁愿不包含 Closure 库。

The following sample code generates a compiler warning on advanced optimization: "JSC_UNSAFE_NAMESPACE: incomplete alias created for namespace NS". If I remove the @enum comment, it doesn't give the warning.

var NS = {};

/**
 * @enum {string}
 */
NS.type = {
    FOO : 'bar'
};

NS.foobar = function(){ alert(NS.type.FOO); };

window['NS'] = NS;
window['NS']['foobar'] = NS.foobar;

Exporting only the function and not the namespace also seems to work:

window['NS_foobar'] = NS.foobar;

What am I doing wrong? Is there a way around this? I'd rather not include the Closure library if possible.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

冷心人i 2024-12-23 16:49:43

编译器希望将枚举值折叠为单个变量:

NS.type.FOO 变为 NS$type$FOO。您导出的“NS”不会包含您期望的内容。

我怀疑你想要这样的东西:

window['NS'] = {}; // an external namespace object.
window['NS']['foobar'] = NS.foobar; // add 'foobar' to the external namespace.

The compiler expects to collapse the enum value into single variables:

NS.type.FOO becomes NS$type$FOO. The "NS" that you exported would not contain what you expect.

I suspect you want something like this:

window['NS'] = {}; // an external namespace object.
window['NS']['foobar'] = NS.foobar; // add 'foobar' to the external namespace.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文