简体   繁体   English

在CoffeeScript中,如何将函数作为函数的参数传递,而函数又需要一个参数?

[英]In CoffeeScript, how do you pass a function as an argument of a function, which also in turn takes an argument?

I don't know how to phrase it properly so I can't find answers by Google, but here's basically my problem: 我不知道如何正确地表达它的意思,因此我找不到Google的答案,但这基本上是我的问题:

I want my CoffeeScript to output something like this in JS: (I'm developing a Node app) 我希望我的CoffeeScript在JS中输出如下内容:(我正在开发Node应用程序)

var someapp = require('someapp')
var another = require('another')

someapp.configure(function() {
    someapp.use(another.do('argument'));
});

So I wrote it this way in CoffeeScript: 所以我在CoffeeScript中这样写:

someapp = require 'someapp'
another = require 'another'

someapp.configure () ->
    someapp.use another.do 'argument'

But instead, I'm getting this output: 但是,相反,我得到以下输出:

some.configure(function() {
    return someapp.use(another["do"]('argument'));
});

Obviously, my biggest problem is the line return someapp.use(another["do"]('argument')); 显然,我最大的问题是该行return someapp.use(another["do"]('argument')); I can't find in the CoffeeScript docs or elsewhere the proper syntax, so I'm hoping someone can point me to the right direction. 我在CoffeeScript文档或其他地方找不到正确的语法,所以我希望有人可以指出正确的方向。 Thanks in advance. 提前致谢。

According to the docs , 根据文档

CoffeeScript provides the do keyword, which immediately invokes a passed function, forwarding any arguments. CoffeeScript提供了do关键字,该关键字立即调用传递的函数,并转发所有参数。

So, coffeescript outputs another["do"] to avoid using the do reserved keyword. 因此,coffeescript输出another["do"]以避免使用do reserved关键字。

Furthermore, in this case, the function another.do is an object property that happens to be a function. 此外,在这种情况下,函数another.do是碰巧是函数的对象属性。 It can be accessed by using both another.do() and another["do"]() . 可以同时使用another.do()another["do"]()来访问它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM