Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
294 views
in Technique[技术] by (71.8m points)

javascript - How to pass two anonymous functions as arguments in CoffeScript?

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.

$('element').hover(
  -> console.log("first")
  -> console.log("second")
)

Or with comments using /* .. */.

$('element').hover(
  -> /* first */
  -> /* second */
)

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:

$('element').hover(
  () ->
  () ->
)
// $('element').hover(function() {}, function() {});

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...