问题
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