我總是想知道爲什麼js壓縮機不這樣做。比方說,我有這樣一個縮小的腳本:爲什麼js壓縮機不這樣做?
(function($){var a=$("a");1<a.length&&10>a.length?alert(a.length):alert($("p").length)})(jQuery);
爲什麼不把它編譯成這樣的:
(function($,L){var a=$("a");1<a[L]&&10>a[L]?alert(a[L]):alert($("p")[L])})(jQuery,'length');
含義改變.property
的每次出現與[minified key]
並傳遞一個字符串, ARG接收到iife
(基本上是我與.length
前面所做的那樣)想象一下,多少這將運行如下的jQuery 159 .length
,62 .each
,15 .appendChild
等。僅僅.length
就能節省超過600字節!
而且使用上面爲什麼它在var關鍵字浪費空間時,你可以保留一個空間中,通過聲明另一個參數功能範圍的例子:
(function($,L,a){a=$("a");1<a[L]&&10>a[L]?alert(a[L]):alert($("p")[L])})(jQuery,'length');
而且雖然我有你在身邊,爲什麼不把整個jQuery進入eval並保存在另一個572 function
關鍵字(4.5k)?
我曾經手寫過這樣的JS,有時候還是這樣。還可以使用String.prototype.IO = String.prototype.indexOf或Us ='undefined'; Ss ='string'; Ow = window;和String.prototype.has(s) – 2012-01-15 04:22:40