LoginSignup
44
28

More than 5 years have passed since last update.

波括弧で引数をくくる「引数分割束縛」について

Posted at

発端

Vuexのドキュメントにあった
https://vuex.vuejs.org/ja/actions.html

ググラビリティが低めなのでこまった。
なんとか理解できたので、ググラビリティをあげるために投稿

what

これが

increment (context) {
  context.commit('increment')
}

こうかける

increment ({ commit }) {
  commit('increment')
}

つまり何

あらかじめ引数を{commit}として渡しておくことで、その関数ブロックの中で呼び出そうとした関数とか変数とかはcommitオブジェクトのプロパティとして解決される。
なので、いちいちcontext.commit()とかしなくていい。

何が幸せ

すっきりかける。
そのオブジェクトのプロパティを使えば使うほどすっきりかける。

参考文献

https://javascript.info/destructuring-assignment#object-destructuring
https://sekaiokaeru.com/tips/javascript-argument-destructuring

44
28
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
44
28