LoginSignup
2
2

More than 3 years have passed since last update.

JavaScript Objectの省略記法について

Posted at

オブジェクトを簡潔に書く

JavaScript(ECMAScript2015以降)のオブジェクトは値とキーが一致しているときは簡潔に書くことができます。

例えばreduxのaction creatorを書くときは以下のような書き方をします。

let nextTodoId = 0;
export const addTodo = text => ({
  type: ADD_TODO,
  id: nextTodoId++,
  text
});

注目して欲しいのは最後のtextの部分なのですが、キーがありません。

上記はショートハンドを使わなければ以下のように書くことになります。

let nextTodoId = 0;
export const addTodo = text => ({
  type: ADD_TODO,
  id: nextTodoId++,
  text: text
});

以上です。

2
2
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
2
2