1
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で既存のオブジェクトにプロパティを足したオブジェクトを作る

Posted at

以前は

const newObject = Object.assign({newProp:'hoge'},oldObject);

とObject.assignを使って書いていたのですが、


const newObject2 = {newProp:'hoge',...oldObject};

とした方がスマートな気がしてこちらを最近は使っています。
スプレッド構文を使うと、


const newObject3 = {...oldObject,newProp:'hoge'};

とすることで、newPropがすでにoldObjectにあっても、oldObjectを汚さずに上書きできるのも良い感じです。
(Object.assignは第一引数のObjectを書き換えてしまう)

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