0
0

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.

既存のJavaScriptをCoffeeSrcriptに書き換えたときにわかったことメモ

Posted at

GulpfileをJSからCoffeeScriptに書き換えました

ちょっと前の話なので、書き換えたときにはまった部分を個人的にメモ。

関数呼び出しの時の()は不要だけど、引数なしの時は必要

☓ func
◯ func()

関数呼び出しの結果をまとめる時注意

func1 func2 arg1, arg2
  ↓
func1(func2(arg1, arg2));

func1 func2(arg1), arg2
  ↓
func1(func2(arg1), arg2);

===isを使う

func1() if val is undefined
  ↓
if (val === void 0) {
    func1();
}

連想配列の初期化は結構便利

hash1 = key1: 'val1', key2: ['val2'], key3: key31: 'val31'
  ↓
hash1 = {
  key1: 'val1',
  key2: ['val2'],
  key3: {
    key31: 'val31'
  }
};

でも改行を入れたほうがわかりやすい(ついでに末尾にカンマ打ちまくってもOK)

hash2 =
  key1: 'val1',
  key2: ['val2'],
  key3:
    key31: 'val31',
    key32: 'val32',
  key4: 'val4',
  ↓
hash2 = {
  key1: 'val1',
  key2: ['val2'],
  key3: {
    key31: 'val31',
    key32: 'val32'
  },
  key4: 'val4'
};

三項演算子はちょっと違う(「:」が上述の初期化と誤爆する)

val3 = if val1 > 1 then val1 else val2
  ↓
val3 = val1 > 1 ? val1 : val2;

JSにコンパイルして確認する

基本的にRubyの書き方が色々と適応できます。気になったら、スクリプトを書いてみて、以下のコマンドでJSファイルにコンパイルして、確認してみるとわかりやすいかと思います。

./node_modules/.bin/coffee -c xxx.coffee
0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?