Method chaining with function arguments

前端 未结 4 1593
囚心锁ツ
囚心锁ツ 2021-01-30 12:40

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

var req = $.get(\'foo.htm\')
         


        
4条回答
  •  忘掉有多难
    2021-01-30 13:00

    Using the latest CoffeeScript, the following:

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

提交回复
热议问题