问题描述
在 CoffeeScript 中,这个:
In CoffeeScript, this:
class Foo
method: (x) ->
x+1
编译为:
// Generated By CoffeeScript
Foo = (function() {
function Foo() {}
Foo.prototype.method = function(x) {
return x+1;
}
return Foo;
})()
这似乎有点过分了.以下内容应功能相同:
Which seems a bit excessive. The following should be functionally identical:
// Generated by Dave
function Foo() {}
Foo.prototype.method = function(x) {
return x+1;
}
额外的闭包"包装器的动机是什么?
这不仅仅是样式问题.它对整体代码大小有影响.
This is not merely an idle question of styling; it has implication to overall code size.
Coffee 版本缩小为 84 个字节:
The Coffee version minifies into 84 bytes:
Foo=function(){function e(){}return e.prototype.method=function(e){return e+1},e}();
我的版本缩小到只有 61 个字节:
My version minifies into only 61 bytes:
function Foo(){}Foo.prototype.method=function(e){return e+1};
23 字节是愚蠢的无关紧要,但在一个有很多类的项目中,开销开始增加.
好的,我在下面写了一个反驳字节大小理论的答案......对于任何合理的类,Coffee 方法都会更小.
Ok, I wrote an answer below refuting the byte size theory ... for any reasonable class, the Coffee method is going to be smaller.
可能还有其他原因.帮我想想它们.
There's probably other reasons too. Help me think of them.
推荐答案
Jeremy 在相关问题中回答了这个问题 -看起来主要目的是避免触发 IE 错误.
Jeremy answers this over in a related question - it looks like the primary intent is to avoid triggering an IE bug.
这篇关于为什么 CoffeeScript 将类定义包装在闭包中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!



大气响应式网络建站服务公司织梦模板
高端大气html5设计公司网站源码
织梦dede网页模板下载素材销售下载站平台(带会员中心带筛选)
财税代理公司注册代理记账网站织梦模板(带手机端)
成人高考自考在职研究生教育机构网站源码(带手机端)
高端HTML5响应式企业集团通用类网站织梦模板(自适应手机端)