Created
April 1, 2017 11:48
-
-
Save crongro/dce63485f77fee65fd539fb9943ae479 to your computer and use it in GitHub Desktop.
es6_spread_operator
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
//참조를 유지하기 때문에 이전데이터와 현재데이터는 당연히 같다. | |
var originData = [1,2,"foo","bar"]; | |
var newData = originData; | |
console.log(originData === newData) //true | |
newData[0] = 3; | |
console.log(newData[0] === originData[0]); //true | |
//이전데이터와 새로운데이터간의 비교가 필요하다면, 참조를 유지하지 않고 새로운 데이터를 만들어내야 한다. | |
//immutable array 를 유지하는 방법. | |
var originData = [1,2,"foo","bar"]; | |
var newData = originData.concat([]); | |
console.log(originData === newData) //false; | |
newData[0] = 3; | |
console.log(originData[0] === newData[0]); //false | |
//ES6 spread operator로 동일하게 해결하기. | |
var originData = [1,2,"foo","bar"]; | |
var newData = [...originData]; | |
originData[3] === newData[3]; | |
console.log(originData === newData) //false; | |
newData[0] = 3; | |
console.log(originData[0] === newData[0]); //false |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment