Closure Compiler发出带有命名空间枚举的警告 [英] Closure Compiler issues warning with namespaced enum

查看:65
本文介绍了Closure Compiler发出带有命名空间枚举的警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下示例代码在高级优化时生成编译器警告:"JSC_UNSAFE_NAMESPACE:为名称空间NS创建的别名不完整".如果删除@enum注释,则不会发出警告.

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;

我做错了什么?有没有解决的办法?如果可能的话,我宁愿不包括Closure库.

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

推荐答案

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

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

NS.type.FOO变为NS $ type $ FOO.您导出的"NS"将不包含您的期望.

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.

这篇关于Closure Compiler发出带有命名空间枚举的警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆