Skip to content

Instantly share code, notes, and snippets.

@ro31337
Last active October 22, 2018 23:02
Show Gist options
  • Select an option

  • Save ro31337/08e6809143bcbd1b82fc67f5a8b4a396 to your computer and use it in GitHub Desktop.

Select an option

Save ro31337/08e6809143bcbd1b82fc67f5a8b4a396 to your computer and use it in GitHub Desktop.
Create new object in JavaScript, Object.assign vs destructuring

Define state:

> state = { aa: 11, bb: 22 }
{ aa: 11, bb: 22 }

First way, create new object with Object.assign:

> Object.assign({}, state, { aa: 99, cc: 33 })
{ aa: 99, bb: 22, cc: 33 }

Second way, use destructuring to create new object with the same properties:

> {...state, aa: 99, cc: 33 }
{ aa: 99, bb: 22, cc: 33 }

Make sure state is the same:

> state
{ aa: 11, bb: 22 }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment