主要的JavaScript壓縮器和縮小器不處理對象屬性名稱。 (Google Closure,YUI ...)JavaScript壓縮和對象屬性問題
我注意到,根據我們決定爲我們的腳本選擇哪種方式或模式,結果大小有很大差異(gzipped和不gzipped)。例如,爲我們的項目選擇原型模式很可能會生成更大的結果文件(未壓縮,壓縮和gzip)。
下面是用兩個代碼部分恰好做同樣的一點比較:
- Prototype pattern138字節 gzip壓縮(286個字節的未壓縮)
- Without pattern87字節 gzip壓縮(110個字節未壓縮)
使用Google Closure compiler壓縮。
原型圖案
變種MyBluePrint =函數(){this.name = 「演示」:
結果通過看所得到的壓縮代碼是很明顯; this.someFunction = function(){alert(「some function」)}; this.someOtherFunction = function(){alert(「some other function」)}; this.showMyName = function(){alert(this.name) }; this.someFunction(); this.someOtherFunction(); this.showMyName()}; new MyBluePrint;
無圖案
變種MyBluePrint =函數(){警報( 「一些功能」);警報( 「一些其他功能」);警報( 「演示」 )}; new MyBluePrint;使用對象屬性
任何不會被壓縮。如:
//function declarations
this.someFunction = function(){ ... }
//objects
var demo = {
isActive: 'aaaa'.
name: 'aaaa'
}
我們是否應該在創建項目時真正考慮這一點? 這不是第一次,而不是有一個對象完整的屬性,我決定使用正常變量只是爲了壓縮多次使用長名稱的屬性。
取決於潛在的節省,並因此取決於代碼的最小化。最終的節省有多大? – tucuxi
你的JSFiddle鏈接是相同的。兩者似乎都是「無模式」的方法。 –
@ChadKillingsworth對不起。更新的鏈接。 – Alvaro