LoginSignup
3
0

More than 5 years have passed since last update.

javascript ...の基本的な使い方

Last updated at Posted at 2017-12-04

参考 
http://yami-beta.hateblo.jp/entry/2016/09/06/112330

reactでは以下のような...の記法を見かけます。

render() {
  return <Counter {...this.state} />
}

これはSpread AttributesというJSXの記法

子の Component へ props を渡す場合,Spread Attributes を用いると,以下のように書き換えられます.

1つずつ渡す場合

const props = { foo: "foo", bar: "bar" };
render() {
  return <Child foo={props.foo} bar={props.bar} />
}

Spread Attributes を用いて渡す場合

const props = { foo: "foo", bar: "bar" };
render() {
  return <Child {...props} />
}

ES.next でのスプレッド演算子
「...」(ドット3つ)の記法は,スプレッド演算子として ES2015 で Array に導入されています.

let ary1 = ["first", "second"];
let ary2 = [...ary1, "third"]; // ["first", "second", "third"]

ES.next では,「...」(ドット3つ)の記法を,Object で使える Object Rest/Spread Properties が提案されています.

let obj1 = { x: 1, y: 2 };
let obj2 = { ...obj1, z: 3 }; // { x: 1, y: 2, z: 3 }

これを用いると,以下のような Object.assign() を用いていたコードが簡潔に書けるようになります.

let obj1 = { x: 1, y: 2 };
let obj2 = Object.assign({}, obj1, { z: 3 });
3
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
3
0