<tfoot id='nybq5'></tfoot>
  1. <legend id='nybq5'><style id='nybq5'><dir id='nybq5'><q id='nybq5'></q></dir></style></legend>

    1. <small id='nybq5'></small><noframes id='nybq5'>

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

      顶级变量不是全局范围的,在 CoffeeScript 中返回值是强制性的

      Top-level variables aren#39;t globally-scoped and return values are mandatory in CoffeeScript(顶级变量不是全局范围的,在 CoffeeScript 中返回值是强制性的)
          <tfoot id='dvVun'></tfoot>
            <bdo id='dvVun'></bdo><ul id='dvVun'></ul>
              <tbody id='dvVun'></tbody>

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

            <legend id='dvVun'><style id='dvVun'><dir id='dvVun'><q id='dvVun'></q></dir></style></legend>
              1. <i id='dvVun'><tr id='dvVun'><dt id='dvVun'><q id='dvVun'><span id='dvVun'><b id='dvVun'><form id='dvVun'><ins id='dvVun'></ins><ul id='dvVun'></ul><sub id='dvVun'></sub></form><legend id='dvVun'></legend><bdo id='dvVun'><pre id='dvVun'><center id='dvVun'></center></pre></bdo></b><th id='dvVun'></th></span></q></dt></tr></i><div id='dvVun'><tfoot id='dvVun'></tfoot><dl id='dvVun'><fieldset id='dvVun'></fieldset></dl></div>
              2. 本文介绍了顶级变量不是全局范围的,在 CoffeeScript 中返回值是强制性的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                funName = () ->
                  $(".foo").addClass("bar");
                

                Compiles into the scope of an anonymous function. Calling funName from the console results in undefined.

                (function() {
                  var funName;
                  funName = function() {
                    return $(".foo").addClass("bar");
                  };
                }).call(this);
                

                What's its reasoning for compiling like this and how do I work with it?

                Also any insight on the mandatory return within functions using CoffeeScript would be great. Why is it like that? How do I need to code differently because of it?

                解决方案

                Mike has answered the main question here. The modular wrapper a common point of confusion for CoffeeScript newcomers, as illustrated by these related questions:

                • How do I define global variables in CoffeeScript?
                • Why use the javascript function wrapper (added in coffeescript) ".call(this)"
                • Getting rid of CoffeeScript's closure wrapper

                As to your other question: If you don't want a function to return anything, simply make the last line of that function either return by itself or, equivalently, undefined. Either will compile to a function with no return. For instance:

                funName = ->
                  $(".foo").addClass "bar"
                  return
                

                compiles to

                var funName;
                funName = function() {
                  $(".foo").addClass("bar");
                };
                

                Note that there is an ongoing discussion (issue 899) about a possible alternative syntax for defining no-return functions. If the current proposal were accepted, you'd be able to write your function as

                funName = -/> $(".foo").addClass "bar"
                

                If you like that syntax, you should voice your support for it.

                这篇关于顶级变量不是全局范围的,在 CoffeeScript 中返回值是强制性的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                Rails/Javascript: How to inject rails variables into (very) simple javascript(Rails/Javascript:如何将 rails 变量注入(非常)简单的 javascript)
                quot;Each child in an array should have a unique key propquot; only on first time render of page(“数组中的每个孩子都应该有一个唯一的 key prop仅在第一次呈现页面时)
                Rails 3.1 ajax:success handling(Rails 3.1 ajax:成功处理)
                CoffeeScript always returns in anonymous function(CoffeeScript 总是以匿名函数返回)
                Ordinals in words javascript(javascript中的序数)
                getFullYear returns year before on first day of year(getFullYear 在一年的第一天返回前一年)

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

                1. <legend id='Tqe16'><style id='Tqe16'><dir id='Tqe16'><q id='Tqe16'></q></dir></style></legend>
                      <tbody id='Tqe16'></tbody>
                  • <tfoot id='Tqe16'></tfoot>

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