javascript CoffeeScript,传递多个参数,包括一个匿名函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10129909/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 08:46:49  来源:igfitidea点击:

CoffeeScript, pass multiple parameters including an anonymous function

javascriptcoffeescript

提问by d4rklit3

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

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

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

thanks.

谢谢。

回答by scottheckel

You would write some CoffeeScript like so...

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

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

Which would convert to the JavaScript like so...

哪个会像这样转换为 JavaScript...

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

回答by datentyp

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

in JavaScript is:

在 JavaScript 中是:

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

in CoffeeScript.

在 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:

在 CoffeeScript 中等于:

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

in JavaScript.

在 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:

在 CoffeeScript 中编译为:

var hello;

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

hello("Coffee", "Script");

in JavaScript.

在 JavaScript 中。

回答by SethWhite

Another option:

另外一个选择:

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