Closure Compiler issues warning with namespaced enum

折月煮酒 提交于 2019-12-07 16:19:37

问题


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.


回答1:


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.


来源:https://stackoverflow.com/questions/8178382/closure-compiler-issues-warning-with-namespaced-enum

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!