CoffeeScript 方法还具有缩小的优势。
从我的另一个答案:
对于大多数合理的类,CoffeeScript 生成的闭包会生成更小的缩小输出。
闭包包装器减少了 25 个字节的开销,但它使您免于重复 classname,节省k * N
字节(k=letters-in-name,N=num-of-refs)。例如,如果一个类似的类BoilerPlateThingyFactory
有 2 个以上的方法,则闭包包装器会生成更小的压缩代码。
更详细...
Coffee 使用闭包生成的代码缩小为:
// Uglify '1.js' = 138 bytes (197 w/ whitespace):
var Animal=function(){function e(e){this.name=e}return e.prototype.speak=function(e){return"My name is "+this.name+" and I like "+e},e}();
// with whitespace ("uglifyjs -b"):
var Animal = function() {
function e(e) {
this.name = e;
}
return e.prototype.speak = function(e) {
return "My name is " + this.name + " and I like " + e;
}, e;
}();
ryeguy 的替代“惯用”实现缩小到这一点:
// Uglify '2.js' = 119 bytes (150 w/ whitespace):
var Animal=function(t){this.name=t};Animal.prototype.speak=function(e){return"My name is "+this.name+" and I like "+e};
// with whitespace ("uglifyjs -b"):
var Animal = function(t) {
this.name = t;
};
Animal.prototype.speak = function(e) {
return "My name is " + this.name + " and I like " + e;
};
请注意名称“动物”名称在 Coffee 形式中恰好存在一次,而在 ryeguy 的“惯用”变体中 N=2 次。现在“Animal”只有 6 个字母,而且只有 1 个方法,所以这里的 Coffee 应该会丢失 25-6 = 19 个字节。咨询我的缩小代码,它是 138 字节到 119 字节,增量为 ... 19 字节。再增加4个方法,优势会切换到Coffee。这不仅仅是方法;类常量和其他 ref 类型也很重要。