Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CoffeeScript, pass multiple parameters including an anonymous function

I am not sure how to write this in CS. maybe some1 can help:

FB.getLoginStatus(function (response) {} , {scope : scope})

thanks.

like image 758
d4rklit3 Avatar asked Apr 12 '12 18:04

d4rklit3


2 Answers

You would write some CoffeeScript like so...

FB.getLoginStatus(
  (response) -> 
    doSomething()
  {scope: scope})

Which would convert to the JavaScript like so...

FB.getLoginStatus(function(response) {
  return doSomething();
}, {
  scope: scope
});
like image 69
scottheckel Avatar answered Sep 19 '22 11:09

scottheckel


FB.getLoginStatus(function(response) {}, {
  scope: scope
});

in JavaScript is:

FB.getLoginStatus(
  (response) ->
  { scope }
)

in CoffeeScript.

To answer your question about multiple parameters further have a look at these examples:

$('.main li').hover(
  -> $(@).find('span').show()   
  -> $(@).find('span').hide()
)

In CoffeeScript equals to:

$('.main li').hover(function() {
  return $(this).find('span').show();
}, function() {
  return $(this).find('span').hide();
});

in JavaScript.

An even simpler example regarding handling multiple parameters (without anonymous functions) would be:

hello = (firstName, lastName) ->
  console.log "Hello #{firstName} #{lastName}"

hello "Coffee", "Script"

in CoffeeScript compiles to:

var hello;

hello = function(firstName, lastName) {
  return console.log("Hello " + firstName + " " + lastName);
};

hello("Coffee", "Script");

in JavaScript.

like image 25
datentyp Avatar answered Sep 18 '22 11:09

datentyp