11
11

More than 5 years have passed since last update.

簡易メソッドチェーン

Last updated at Posted at 2013-01-22

各メソッドの最後に@(=this)を返すだけです

worker.coffee
class Worker
  doThis: () ->
    console.log 'this'
    return @
  doThat: () ->
    console.log 'that'
    return @
  doSomethingElse: () ->
    console.log 'somethingelse'
    return @

worker = new Worker()
worker.doThis()
      .doThat()
      .doSomethingElse()

実行してみると

$ coffee worker.coffee 
this
that
somethingelse
11
11
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
11
11