最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Why does Google Closure Compiler adds variable to the global namespace when the original namespace was empty? - Sta

programmeradmin3浏览0评论

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 piler starts by redefining i and j in the global namespace which feels both unnecessary and dangerous, especially since I am piling a script that has zero interference with the namespace. (the piled script starts with var i=null,j=!1;, for pactness 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?

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 piler starts by redefining i and j in the global namespace which feels both unnecessary and dangerous, especially since I am piling a script that has zero interference with the namespace. (the piled script starts with var i=null,j=!1;, for pactness 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?

Share Improve this question edited Aug 27, 2013 at 8:00 Mad Echet asked Aug 26, 2013 at 17:20 Mad EchetMad Echet 3,8817 gold badges31 silver badges44 bronze badges 7
  • This is a similar example – abc123 Commented Aug 26, 2013 at 17:32
  • possible duplicate of How to prevent Closure Compiler from renaming "true", "false" and "null" – Alexander Commented Aug 26, 2013 at 17:38
  • Not quite, I already knew about the wrapper trick, which I mention in my post. I was looking for a reason to add a global scope while mine was empty. The answer seems to be, Closure piler does not car whether my scope was empty or not, it just makes the assumption that it can use it. – Mad Echet Commented Aug 26, 2013 at 17:48
  • I edited the title to be more specific about what I was looking for. – Mad Echet Commented Aug 26, 2013 at 17:50
  • btw, there is an option to disable this global vars - closure-piler.googlecode./svn/trunk/javadoc//google/… – dragn Commented Apr 22, 2014 at 16:47
 |  Show 2 more ments

1 Answer 1

Reset to default 10

The piler 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:

When using Advanced Optimizations, Closure Compiler adds new variables to the global scope. How do I make sure my variables don't collide with other scripts on the page?

Closure Compiler's advanced optimizations mode assumes that it's ok to add new variables to the global scope.

In JavaScript, it's often standard practice to wrap your code in an anonymous function, so that variables don't pollute the global scope. Closure Compiler has an --output_wrapper flag for exactly this purpose. Invoking it as --output_wrapper "(function() {%output%})();" will wrap your code in an anonymous function at pile-time.

Closure Compiler users often find it easier and simpler to do this wrapping at pile-time, rather than writing the anonymous function wrapper in the original source code.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论