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

      <tfoot id='wB8OD'></tfoot>

        <legend id='wB8OD'><style id='wB8OD'><dir id='wB8OD'><q id='wB8OD'></q></dir></style></legend>
      1. <small id='wB8OD'></small><noframes id='wB8OD'>

          <bdo id='wB8OD'></bdo><ul id='wB8OD'></ul>

        按名称调用函数

        时间:2024-04-19

      2. <small id='6D53k'></small><noframes id='6D53k'>

                <legend id='6D53k'><style id='6D53k'><dir id='6D53k'><q id='6D53k'></q></dir></style></legend><tfoot id='6D53k'></tfoot>
                  <tbody id='6D53k'></tbody>
                • <bdo id='6D53k'></bdo><ul id='6D53k'></ul>
                  <i id='6D53k'><tr id='6D53k'><dt id='6D53k'><q id='6D53k'><span id='6D53k'><b id='6D53k'><form id='6D53k'><ins id='6D53k'></ins><ul id='6D53k'></ul><sub id='6D53k'></sub></form><legend id='6D53k'></legend><bdo id='6D53k'><pre id='6D53k'><center id='6D53k'></center></pre></bdo></b><th id='6D53k'></th></span></q></dt></tr></i><div id='6D53k'><tfoot id='6D53k'></tfoot><dl id='6D53k'><fieldset id='6D53k'></fieldset></dl></div>
                • 本文介绍了按名称调用函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  有时我们需要通过函数名来调用函数.我可以用纯 JavaScript 来完成,如下所示:

                  Sometimes we need to call a function by its name. I can do it in plain JavaScript as below:

                  global=this
                  function add(a,b){return a+b}
                  global['add'](1,2)
                  

                  按预期工作,add() 被调用.

                  Which works as expected and add() gets called.

                  等效的 CoffeeScript 代码可以写成如下.

                  Equivalent CoffeeScript code might can be written as below.

                  global=@
                  add=(a,b)->a+b
                  global['add'](1,2)
                  

                  编译成 JavaScript 为:

                  which compiles to JavaScript as:

                  (function() {
                    var add, global;
                    global = this;
                    add = function(a, b) {
                      return a + b;
                    };
                    global['add'](1, 2);
                  }).call(this);
                  

                  ...它不起作用.

                  Microsoft JScript runtime error: Object doesn't support this property or method
                  

                  这个问题有简单的解决方法吗?

                  Is there an easy solution to this problem?

                  注意:

                  1. 我没有在浏览器中运行代码,因此没有窗口对象.但是在普通的 JS 中,我总是可以通过分配 global=this 来捕获全局范围,然后从中获取函数指针.

                  1. I am not running the code in a browser therefore there is no window object. But in plain JS I can always capture the global scope by assigning global=this and then get the function pointer from it.

                  我发现 CoffeeScript 中的一个解决方案是将所有函数声明为像 global.add=[function definition] 这样的全局对象的成员.但是我通常必须将该函数称为 global.add().而且它比必要的样板更多.

                  One solution in CoffeeScript I found is by declaring all functions as member of a global object like global.add=[function definition]. But then I have to normally call the function as global.add(). And it's more boiler plate than necessary.

                  有简单的技巧吗?还是有更简单的解决方案?

                  Is there a simple hack? Or any simpler solution?

                  推荐答案

                  你的 add 是一个局部变量.使用

                  Your add is a local variable. Use

                  @add=(a,b)->a+b
                  

                  将其附加到 global 对象.由于 global 是脚本的全局范围,您仍然可以调用 add 而无需在其前面加上 global..

                  to attach it to the global object. Since global is the global scope of your script you can still call add without prefixing it with global..

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

                  上一篇:node.js、backbone、socketio 和 express 和 coffee 脚本都在一起吗? 下一篇:将属性动态分配给 JS 对象

                  相关文章

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

                      <small id='7k6WK'></small><noframes id='7k6WK'>