Created
March 23, 2019 10:46
-
-
Save buglessir/cfed9d06a0459fdefcf9d5eaccb8d5f0 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| function immutable(originalArray) { | |
| // Instead of mutating/modifying the original array, | |
| // we first make a copy of the original array | |
| // In this way, the original array stay unchanged. | |
| var newArray = [...originalArray]; | |
| newArray[0] = "new value"; | |
| return newArray; | |
| } | |
| var array = ["some value", "another value"]; | |
| alert("Return from immutable " + immutable(array)); | |
| alert("Array: " + array + " (original array stay unchanged)."); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment