LoginSignup
75

More than 5 years have passed since last update.

posted at

updated at

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

これまで 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);

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

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
What you can do with signing up
75