Skip to content

Instantly share code, notes, and snippets.

@dleitee
Created April 27, 2017 19:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dleitee/8f1d9ba6a17fb1f430f0e77907eec462 to your computer and use it in GitHub Desktop.
Save dleitee/8f1d9ba6a17fb1f430f0e77907eec462 to your computer and use it in GitHub Desktop.
Immutable Code
const user = {
name: 'John Due',
birthdate: '1988-08-15',
}
const changeName = (user, newName) => {
return {
...user, // destructuring user object
name: newName, // override name attribute with new name
}
}
const userWithNewName = changeName(user, 'Jojo')
// the old user object has name = 'John Due' and the new object, name = 'Jojo'
console.log(user, userWithNewName)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment