1. <tfoot id='9efBz'></tfoot>

        <legend id='9efBz'><style id='9efBz'><dir id='9efBz'><q id='9efBz'></q></dir></style></legend>
          <bdo id='9efBz'></bdo><ul id='9efBz'></ul>

        <small id='9efBz'></small><noframes id='9efBz'>

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

        CoffeeScript 中的函数声明

        时间:2024-04-20

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

              <legend id='8AWYG'><style id='8AWYG'><dir id='8AWYG'><q id='8AWYG'></q></dir></style></legend>

                <bdo id='8AWYG'></bdo><ul id='8AWYG'></ul>

                  <tbody id='8AWYG'></tbody>
                <tfoot id='8AWYG'></tfoot>
                  本文介绍了CoffeeScript 中的函数声明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我注意到在 CoffeeScript 中,如果我使用以下方法定义函数:

                  I notice that in CoffeeScript, if I define a function using:

                  a = (c) -> c=1
                  

                  我只能得到函数表达式:

                  var a;
                  a = function(c) {
                      return c = 1;
                  };
                  

                  但是,我个人经常使用函数声明,例如:

                  But, personally I often use function declaration,for example:

                  function a(c) {
                      return c = 1;
                  }
                  

                  我确实使用第一种形式,但我想知道 CoffeeScript 中是否有一种方法可以生成函数声明.如果没有这样的方法,我想知道为什么 CoffeeScript 避免这样做.只要函数在作用域的顶部声明,我认为 JSLint 不会抱怨声明错误.

                  I do use the first form, but I'm wondering if there is a way in CoffeeScript generating a function declaration. If there is no such way, I would like to know why CoffeeScript avoid doing this. I don't think JSLint would holler an error for declaration, as long as the function is declared at the top of the scope.

                  推荐答案

                  CoffeeScript 只在一个地方使用函数声明(又称命名函数"):class 定义.例如,

                  CoffeeScript uses function declarations (aka "named functions") in just one place: class definitions. For instance,

                  class Foo
                  

                  编译成

                  var Foo;
                  Foo = (function() {
                    function Foo() {}
                    return Foo;
                  })();
                  

                  根据FAQ:

                  将此归咎于微软.最初,每个可以为其检索到合理名称的函数都被赋予了一个,但 IE 8 及以下版本存在范围问题,其中命名函数被视为声明和表达式.请参阅 this 了解更多信息.

                  Blame Microsoft for this one. Originally every function that could have a sensible name retrieved for it was given one, but IE versions 8 and down have scoping issues where the named function is treated as both a declaration and an expression. See this for more information.

                  简而言之:粗心地使用函数声明会导致 IE(9 之前)和其他 JS 环境之间的不一致,因此 CoffeeScript 避开了它们.

                  In short: Using function declarations carelessly can lead to inconsistencies between IE (pre-9) and other JS environments, so CoffeeScript eschews them.

                  这篇关于CoffeeScript 中的函数声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:从 JavaScript 文件或 REPL 中 require()'ing CoffeeScript 文件 下一篇:将 JQuery Promise 数组转换为数组的 JQuery Promise 的最简洁方法是什么?

                  相关文章

                  <tfoot id='M8WXC'></tfoot>
                    <bdo id='M8WXC'></bdo><ul id='M8WXC'></ul>

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

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

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