javascript 带有函数参数的方法链

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5144191/
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-25 16:08:53  来源:igfitidea点击:

Method chaining with function arguments

javascriptsyntaxcoffeescript

提问by nicholaides

What's the best way to chain methods in CoffeeScript? For example, if I have this JavaScript how could I write it in CoffeeScript?

在 CoffeeScript 中链接方法的最佳方式是什么?例如,如果我有这个 JavaScript,我如何用 CoffeeScript 编写它?

var req = $.get('foo.htm')
  .success(function( response ){
    // do something
    // ...
  })
  .error(function(){
    // do something
    // ...
  });

回答by a paid nerd

Using the latest CoffeeScript, the following:

使用最新的 CoffeeScript,如下:

req = $.get 'foo.html'
  .success (response) ->
    do_something()
  .error (response) ->
    do_something()

...compiles to:

...编译为:

var req;
req = $.get('foo.html').success(function(response) {
  return do_something();
}).error(function(response) {
  return do_something();
});

回答by Trevor Burnham

There are two approaches you can take: The best "literal" translation to CoffeeScript is (in my opinion)

您可以采用两种方法: CoffeeScript 的最佳“字面”翻译是(在我看来)

req = $.get('foo.htm')
  .success((response) ->
    # do something
  )
  .error( ->
    # do something
  )

The other approach is to move the inline functions "outline," a style that Jeremy Ashkenas (the creator of CoffeeScript) generally favors for non-trivial function arguments:

另一种方法是移动内联函数“大纲”,Jeremy Ashkenas(CoffeeScript 的创建者)通常喜欢这种风格,用于非平凡的函数参数:

onSuccess = (response) ->
  # doSomething

onError = ->
  # doSomething

req = $.get('foo.htm').success(onSuccess).error(onError)

The latter approach tends to be more readable when the successand errorcallbacks are several lines long; the former is great if they're just 1-2 liners.

successerror回调有几行长时,后一种方法往往更具可读性;前者很好,如果它们只是 1-2 个衬垫。

回答by Amir

I sometimes prefer having fewer parentheses as opposed to chaining, so I'd modify Trevor's last example:

有时我更喜欢使用更少的括号而不是链接,所以我会修改 Trevor 的最后一个例子:

req = $.get 'foo.htm'
req.success (response) -> # do something
req.error -> # do something

回答by Ben McCormick

As of Coffeescript 1.7, chaining has been significantly simplified, and you shouldn't need any of the parens-related workarounds mentioned here. Your above example can now be written as

从 Coffeescript 1.7 开始,链接已经得到显着简化,您不需要这里提到的任何与括号相关的解决方法。你上面的例子现在可以写成

req = $.get 'foo.htm'
.success ( response ) ->
  alert "success"
.error ->
  alert "error"

Which compiles to

编译成

var req;

req = $.get('foo.htm').success(function(response) {
  return alert("success");
}).error(function() {
  return alert("error");
});

You can see an explanation of this and other CS 1.7 features here: https://gist.github.com/aseemk/8637896

您可以在此处查看此功能和其他 CS 1.7 功能的说明:https: //gist.github.com/aseemk/8637896