缩小可能与其他脚本一起运行的 JavaScript 代码是否安全?
例如,在激进模式下,Google Closure 会重命名函数。如果有人将我的闭包编译脚本与其他人也使用闭包编译的另一个脚本一起包含,是否可能会发生重命名冲突?
简而言之,我想缩小我的代码,但它将在其他网站上使用,并且我想避免与其他脚本发生冲突。
For example, in aggressive mode, Google Closure will rename functions. If someone includes my closure compiled script alongside another script someone else also compiled with closure, are renaming conflicts likely to occur?
In short, I want to minify my code, but it will be used on other websites and I want to avoid conflicts with other scripts.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
无论您是否缩小脚本,您始终必须担心 JavaScript 中全局作用域中定义的变量的冲突。如果您想最大程度地减少冲突的机会,请使用功能闭包包装器来包装代码。
闭包的高级模式只会使潜在的冲突变得更糟,因为它将许多对象编译为新的全局对象(名称类似,例如
a
、b
等)以获得最快的性能。这就是为什么 Closure 的高级模式最适合同时使用所有程序文件,而不是零散地使用。像 ga.js (Google Analytics) 这样的东西被设计成只向全局范围公开少数对象,而其他所有对象都包含在闭包中。文件本身已被积极优化。请注意——它被最小化/未最小化这一事实与碰撞无关。您可以拥有一个包含大量冲突的纯脚本 JavaScript 文件,也可以拥有一个没有冲突的高度优化的脚本。
冲突与缩小或变量重命名无关。您可以通过避免在全局范围内创建对象来避免冲突。未在全局范围内创建的任何其他内容都不会与其他脚本发生冲突。例如,如果您加载另一个覆盖
_gat
或_gaq
全局变量的脚本,您可能会碰撞ga.js
。尝试一下,Google Analytics 将不再起作用。换句话说,ga.js
适用于不同的网页不是,因为它是无冲突的,而是因为它创建了全局变量(即_gat
和_gaq
),其名称不太可能被其他脚本选择。You ALWAYS have to worry about collisions of variables defined in the global scope in JavaScript, REGARDLESS of whether you minify your scripts or not. Use a functional closure wrapper to wrap your code if you want to minimize chances of collision.
Closure's Advanced Mode only makes the potential collisions worse, as it compiles many objects into new global objects (which are named similarly, e.g.
a
,b
etc.) for the fastest performance. That's why Closure's Advanced Mode is used best with all the program files at once, never piecemeal.Things like
ga.js
(Google Analytics) are designed so that they expose only a few objects to the global scope and everything else wrapped up in a closure. The file itself is aggressively optimized. Beware -- the fact that it is minimized/not-minimized has nothing to do with collisions. You can have a plain-script JavaScript file with tons of collisions, or you can have a heavily-optimized script with no collisions.Collisions have nothing to do with minification or variable renaming. You avoid collisions by avoiding creating objects in the global scope. Anything else that is not created in the global scope does not collide with other scripts. For example, you CAN collide
ga.js
if you load another script which overwrites the_gat
or_gaq
global variables. Try it, and Google Analytics will no longer work. In other words,ga.js
works with different web pages not because it is collision-free, but because it creates global variables (i.e._gat
and_gaq
) with names that are very unlikely to be chosen by other scripts.你最好使用 YUICompressor,它的攻击性要小得多,并且不会缩小任何看起来是外部的东西。
看看:- http://developer.yahoo.com/yui/compressor/
You'd be better off using YUICompressor, it's a lot less aggressive and tends not to minify anything that looks to be external.
Check it out:- http://developer.yahoo.com/yui/compressor/