如何在 CoffeScript 中传递两个匿名函数作为参数?
我想传递两个匿名函数作为 jQuery 悬停的参数,如下所示:
$('element').hover(
function() {
// do stuff on mouseover
},
function() {
// do stuff on mouseout
}
);
只需一个就很容易 - hover ->
- 但是 CoffeeScript 中两个函数的正确语法是什么?我尝试了 ...hover ->
、...hover( ->...
等,但没有任何东西能让我得到上面的结构。
I want to pass two anonymous functions as arguments for jQuery's hover, like so:
$('element').hover(
function() {
// do stuff on mouseover
},
function() {
// do stuff on mouseout
}
);
It's easy with just one – hover ->
– but what is the proper syntax in CoffeeScript for two? I tried ...hover ->
, ...hover( ->...
, etc. but nothing gets me the above structure.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我认为问题在于使用单行注释
//
。/* .. */
中包含的单行注释似乎工作正常。这是一个等效的示例,其中包含除注释之外的其他内容。或者使用
/* .. */
进行注释。您可以在 尝试 CoffeeScript 选项卡下尝试这些示例。 CoffeeScript 添加了一个 return 语句来返回函数的最后一个表达式。如果您想要不执行任何操作且末尾不包含
return
的简单函数,请尝试:I think the problem lies with using single line comments
//
. Single-line comments enclosed in/* .. */
seem to work fine. Here's an equivalent example with something other than a comment.Or with comments using
/* .. */
.You can try these examples under the Try CoffeeScript tab. CoffeeScript adds a return statement to return the last expression of the function. If you wanted bare-bones functions which do nothing and don't contain a
return
at the end, try:将匿名函数放在括号中。
Put parentheses around the anonymous functions.
另一种方法是在调用函数后使用反斜杠,逗号应正确缩进。
Another way is to use backslash after the caller function, the comma should be indented correctly.
不带括号或反斜杠:
1.7.1 上的输出:
Without parenthesis or backslash:
Output on 1.7.1: