8
1

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 3 years have passed since last update.

JavaScript:ジェネレータをアロー関数で書いとったわ

Last updated at Posted at 2018-11-08

イテレーターとジェネレーター
JavaScript の ジェネレータ を極める!
JSのジェネレーター関数の書き方まとめ

多分普通は

const g = function*(){}

みたいにするんです。きっと。
ぼくもそのつもりで書いていたんですけど、

const g = () => function*(){}()

っていう順番で書いてました。あれ?
他のアロー関数に見た目あわせちゃってました。
これでも動いてるみたいです。

たとえば、配列の要素を返すジェネレータだと:

//文
function* arrayToGen(xs){ 
  yield* xs 
}

//式
const arrayToGen = function*(xs){ 
  yield* xs
}

//アロー関数
const arrayToGen = xs => function*(){ 
  yield* xs
}()

メリット

  • 見た目が好き

デメリット

  • 最後の()を忘れがち

何か問題があればコメントよろしくです。

8
1
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
8
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?