77
75

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

CoffeeScript 1.7.0 でメソッドチェーンがしやすくなった

Last updated at Posted at 2014-01-29

これまで CoffeeScript で jQuery などのメソッドチェーンを使う際、引数を括弧で囲まざるをえず「これじゃあ JS 書いてるのと変わらないじゃないか!」と思っていたみなさん、こんにちは。

CoffeeScript 1.7.0 から、括弧なしでチェーンできるようになりました。

hello.coffee
$ = require 'jquery'

$ '.hello'
  .data 'foo', 'bar'
  .css
    color: '#999'
    fontWeight: 'bold'
  .on 'click', ->
    doSomething()
  .on 'mouseover', ->
    doOtherThing()
  .show()

とか書いて・・・

coffee -c hello.coffee

コンパイルすると・・・

hello.js
// Generated by CoffeeScript 1.7.0
(function() {
  var $;

  $ = require('jquery');

  $('.hello').data('foo', 'bar').css({
    color: '#999',
    fontWeight: 'bold'
  }).on('click', function() {
    return doSomething();
  }).on('mouseover', function() {
    return doOtherThing();
  }).show();

}).call(this);

やった、チェーンできた!めでたしめでたし。

77
75
2

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
77
75

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?