d4rklit3
d4rklit3

Reputation: 427

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.

Upvotes: 6

Views: 11296

Answers (3)

SethWhite
SethWhite

Reputation: 1977

Another option:

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

Upvotes: 0

datentyp
datentyp

Reputation: 1381

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.

Upvotes: 4

scottheckel
scottheckel

Reputation: 9244

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
});

Upvotes: 10

Related Questions