<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 中返回值是强制性的

      时间:2024-04-19
          <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 中返回值是强制性的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:为什么在 Array 的子类中设置位置不会改变它的长度?我不应该继承数组吗? 下一篇:将 Rails 应用程序部署到 Heroku 时出现语法错误:ExecJS::RuntimeError: SyntaxE

                相关文章

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

                1. <legend id='Tqe16'><style id='Tqe16'><dir id='Tqe16'><q id='Tqe16'></q></dir></style></legend>
                2. <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>