<i id='f1ZV9'><tr id='f1ZV9'><dt id='f1ZV9'><q id='f1ZV9'><span id='f1ZV9'><b id='f1ZV9'><form id='f1ZV9'><ins id='f1ZV9'></ins><ul id='f1ZV9'></ul><sub id='f1ZV9'></sub></form><legend id='f1ZV9'></legend><bdo id='f1ZV9'><pre id='f1ZV9'><center id='f1ZV9'></center></pre></bdo></b><th id='f1ZV9'></th></span></q></dt></tr></i><div id='f1ZV9'><tfoot id='f1ZV9'></tfoot><dl id='f1ZV9'><fieldset id='f1ZV9'></fieldset></dl></div>
    • <bdo id='f1ZV9'></bdo><ul id='f1ZV9'></ul>
  1. <small id='f1ZV9'></small><noframes id='f1ZV9'>

      <tfoot id='f1ZV9'></tfoot>

      <legend id='f1ZV9'><style id='f1ZV9'><dir id='f1ZV9'><q id='f1ZV9'></q></dir></style></legend>
    1. 为什么 CoffeeScript 将类定义包装在闭包中?

      时间:2024-04-19
      <i id='KWlGX'><tr id='KWlGX'><dt id='KWlGX'><q id='KWlGX'><span id='KWlGX'><b id='KWlGX'><form id='KWlGX'><ins id='KWlGX'></ins><ul id='KWlGX'></ul><sub id='KWlGX'></sub></form><legend id='KWlGX'></legend><bdo id='KWlGX'><pre id='KWlGX'><center id='KWlGX'></center></pre></bdo></b><th id='KWlGX'></th></span></q></dt></tr></i><div id='KWlGX'><tfoot id='KWlGX'></tfoot><dl id='KWlGX'><fieldset id='KWlGX'></fieldset></dl></div>

          <tfoot id='KWlGX'></tfoot>
            <legend id='KWlGX'><style id='KWlGX'><dir id='KWlGX'><q id='KWlGX'></q></dir></style></legend>

            <small id='KWlGX'></small><noframes id='KWlGX'>

              <tbody id='KWlGX'></tbody>
              • <bdo id='KWlGX'></bdo><ul id='KWlGX'></ul>

              • 本文介绍了为什么 CoffeeScript 将类定义包装在闭包中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                在 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 将类定义包装在闭包中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:是否可以在 JavaScript 中使用对象文字定义动态命名的属性? 下一篇:为什么在 Array 的子类中设置位置不会改变它的长度?我不应该继承数组吗?

                相关文章

                  <bdo id='hVRZE'></bdo><ul id='hVRZE'></ul>
              • <i id='hVRZE'><tr id='hVRZE'><dt id='hVRZE'><q id='hVRZE'><span id='hVRZE'><b id='hVRZE'><form id='hVRZE'><ins id='hVRZE'></ins><ul id='hVRZE'></ul><sub id='hVRZE'></sub></form><legend id='hVRZE'></legend><bdo id='hVRZE'><pre id='hVRZE'><center id='hVRZE'></center></pre></bdo></b><th id='hVRZE'></th></span></q></dt></tr></i><div id='hVRZE'><tfoot id='hVRZE'></tfoot><dl id='hVRZE'><fieldset id='hVRZE'></fieldset></dl></div>
                <tfoot id='hVRZE'></tfoot>

                <small id='hVRZE'></small><noframes id='hVRZE'>

                  <legend id='hVRZE'><style id='hVRZE'><dir id='hVRZE'><q id='hVRZE'></q></dir></style></legend>