多个函数作为CoffeeScript中的参数 [英] Multiple Functions as Arguments in CoffeeScript

查看:132
本文介绍了多个函数作为CoffeeScript中的参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不能为我的生活计算出来或在网上找到解决方案。我试图找出如何在基于jQuery的JavaScript中在CoffeeScript中编写脚本。

I can't for the life of me figure this out or find a solution online. I am trying to figure out how to write a script in CoffeeScript from jQuery based JavaScript.

脚本是这样的:

jQuery('.post-thumb a').hover( function() {
    jQuery(this).find('.overlay').fadeIn(150);
}, function() {
    jQuery(this).find('.overlay').fadeOut(150);
});

我最初尝试像这样重写:

I initially tried rewriting that like this:

thumb_overlay =>
    $('.post-thumb a').hover
        => $(this).find('.overlay').fadeIn(150)
        ,=> $(this).find('.overlay').fadeOut(150)



< t工作,所以我想我会在这里张贴。那么如何在CoffeeScript中编写JavaScript?

But that didn't work, so I thought I would post here. So how do I write that JavaScript in CoffeeScript?

推荐答案

我想你几乎在那里,但你需要一些括号东西)和一些反斜杠,以防止CoffeeScript误解新行。尝试此操作:

I think you're almost there but you need some parentheses (to group things) and some backslashes to keep CoffeeScript from misinterpreting the newlines. Try this:

thumb_overlay =>
    $('.post-thumb a').hover \
        (=> $(this).find('.overlay').fadeIn(150)), \
        (=> $(this).find('.overlay').fadeOut(150))

您也可以将其全部合并成一行,但您可能会在几个月后后悔:

You could also mash it all into one line but you might regret it in a few months:

thumb_overlay =>
    $('.post-thumb a').hover (=> $(this).find('.overlay').fadeIn(150)), (=> $(this).find('.overlay').fadeOut(150))

然后,转到 GitHub页面,并点击TRY COFFEESCRIPT,这是一个简单的方法来排序CoffeeScript的小部分;以 - > 版本开始减少JavaScript中的噪音,然后切换到 => 您获得了正确的JavaScript。

And BTW, go to the GitHub page and hit "TRY COFFEESCRIPT", that's an easy way to sort small bits of CoffeeScript out; start with the -> version to cut down on the noise in the JavaScript and then switch to => when you get the right JavaScript.

我不确定您是否要在这种情况下使用 => - > 表单:

I'm not sure if you want to => forms in this case, the -> form form:

$('.post-thumb a').hover \
    (-> $(this).find('.overlay').fadeIn(150)), \
    (-> $(this).find('.overlay').fadeOut(150))

您先是:

$('.post-thumb a').hover((function() {
  return $(this).find('.overlay').fadeIn(150);
}), (function() {
  return $(this).find('.overlay').fadeOut(150);
}));

如果你不喜欢反斜杠,你可以这样做:

And if you don't like backslashes, you could do this:

$('.post-thumb a').hover( 
    -> $(this).find('.overlay').fadeIn(150)
    -> $(this).find('.overlay').fadeOut(150)
)

这篇关于多个函数作为CoffeeScript中的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆