Created
April 27, 2017 19:42
-
-
Save dleitee/8f1d9ba6a17fb1f430f0e77907eec462 to your computer and use it in GitHub Desktop.
Immutable Code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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