本文介绍了当原始命名空间为空时,为什么Google Closure Compiler会将变量添加到全局命名空间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个很长的脚本很好地包装在(function(){/.../})(/)/ code>中,以避免所有类型的名称污染。它是100%打字,没有警告。



我发现Google Closure编译器首先重新定义 i 和 j 感觉既不必要又危险,特别是因为我正在编译一个对命名空间没有干扰的脚本。 (编译后的脚本以 var i = null,j =!1; 开头,出于紧凑的原因,我相信)。



- output_wrapper 来包装它,但我想不出谷歌会像这样污染命名空间的原因。



有没有?

解决方案

编译器希望给出它所有相关的JavaScript,因此无需担心与其他脚本的冲突。因此,它假定它可以打开不必要的匿名函数。



来自:


I have a long script nicely wrapped into a (function() {/.../})() to avoid all kind of name pollution. It is 100% typed with zero warning.

I found out that Google Closure compiler starts by redefining i and j in the global namespace which feels both unnecessary and dangerous, especially since I am compiling a script that has zero interference with the namespace. (the compiled script starts with var i=null,j=!1;, for compactness reasons I believe).

I can think of a work around which is to wrap it using the --output_wrapper but I can't think of a reason why Google would pollute the namespace like this.

Is there any?

解决方案

The compiler expects that it's given all relevant JavaScript so that it doesn't need to worry about clashes with other scripts. Therefore it assumes that it can unwrap the "unnecessary" anonymous function.

From the FAQ:

这篇关于当原始命名空间为空时,为什么Google Closure Compiler会将变量添加到全局命名空间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-28 03:23
查看更多