【问题标题】:CoffeeScript, pass multiple parameters including an anonymous functionCoffeeScript,传递多个参数,包括匿名函数
【发布时间】:2012-04-25 04:09:04
【问题描述】:

我不知道如何在 CS 中写这个。也许 some1 可以提供帮助:

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

谢谢。

【问题讨论】:

标签: javascript coffeescript


【解决方案1】:

你会像这样写一些 CoffeeScript...

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

这会像这样转换成 JavaScript...

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

【讨论】:

  • 很抱歉我的快速样品不符合您对完美的要求。
  • 你被原谅了。 FB.getLoginStatus receiveLogin, { scope }
【解决方案2】:
FB.getLoginStatus(function(response) {}, {
  scope: scope
});

在 JavaScript 中是:

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

在 CoffeeScript 中。

要回答有关多个参数的问题,请进一步查看以下示例:

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

在 CoffeeScript 中等于:

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

在 JavaScript 中。

关于处理多个参数(没有匿名函数)的一个更简单的例子是:

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

hello "Coffee", "Script"

在 CoffeeScript 中编译为:

var hello;

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

hello("Coffee", "Script");

在 JavaScript 中。

【讨论】:

    【解决方案3】:

    另一种选择:

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

    【讨论】:

      猜你喜欢
      • 2012-07-20
      • 1970-01-01
      • 2011-09-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-23
      • 2015-05-09
      相关资源
      最近更新 更多