Use JavaScripte References, Copying
์ด๊ธฐ ์ฝ๋
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<title>JS Reference VS Copy</title>
</head>
<body>
<script>
// start with strings, numbers and booleans
// ๋ฌธ์์ด, ์ซ์ ๋ฐ ๋ถ์ธ๋ก ์์
// Let's say we have an array
// ๋ฐฐ์ด์ด ์๋ค๊ณ ๊ฐ์ ํด ๋ด
์๋ค.
const players = ['Wes', 'Sarah', 'Ryan', 'Poppy'];
// and we want to make a copy of it.
// ๋ณต์ฌ๋ณธ์ ๋ง๋ค๊ณ ์ถ์ต๋๋ค.
// You might think we can just do something like this:
// ๋ค์๊ณผ ๊ฐ์ด ํ ์ ์๋ค๊ณ ์๊ฐํ ์๋ ์์ต๋๋ค.
// however what happens when we update that array?
//
// now here is the problem!
// ์ด์ ์ฌ๊ธฐ์ ๋ฌธ์ ๊ฐ ์์ต๋๋ค!
// oh no - we have edited the original array too!
// ์๋จ-์ฐ๋ฆฌ๋ ์๋ ๋ฐฐ์ด๋ ํธ์งํ์ต๋๋ค!
// Why? It's because that is an array reference, not an array copy. They both point to the same array!
// ์? ๊ทธ๊ฒ์ ๋ฐฐ์ด ๋ณต์ฌ๊ฐ ์๋๋ผ ๋ฐฐ์ด ์ฐธ์กฐ์ด๊ธฐ ๋๋ฌธ์
๋๋ค. ๋ ๋ค ๋์ผํ ๋ฐฐ์ด์ ๊ฐ๋ฆฌ ํต๋๋ค!
// So, how do we fix this? We take a copy instead!
// ๊ทธ๋์ ์ด๋ป๊ฒ ๊ณ ์น ๊น์? ๋์ ๋ณต์ฌํฉ๋๋ค!
// one way
// or create a new array and concat the old one in
// ๋๋ ์ ๋ฐฐ์ด์ ๋ง๋ค๊ณ ์ด์ ๋ฐฐ์ด์
// or use the new ES6 Spread
// ๋๋ ์๋ก์ด ES6 ์คํ๋ ๋ ์ฌ์ฉ
// now when we update it, the original one isn't changed
// ์ด์ ์
๋ฐ์ดํธ ํ ๋ ์๋ณธ์ ๋ณ๊ฒฝ๋์ง ์์ต๋๋ค
// The same thing goes for objects, let's say we have a person object
// ๊ฐ์ฒด๋ ๋ง์ฐฌ๊ฐ์ง์
๋๋ค. ์ฌ๋ ๊ฐ์ฒด๊ฐ ์๋ค๊ณ ๊ฐ์ ํด ๋ณด๊ฒ ์ต๋๋ค.
// with Objects
const person = {
name: 'Wes Bos',
age: 80
};
// and think we make a copy:
// ์ฌ๋ณธ์ ๋ง๋ ๋ค๊ณ ์๊ฐํฉ๋๋ค.
// how do we take a copy instead?
// ๋์ ๋ณต์ฌํ๋ ๋ฐฉ๋ฒ์ ๋ฌด์์
๋๊น?
// We will hopefully soon see the object ...spread
// ์ฐ๋ฆฌ๋ ๊ณง ๊ทธ object๋ฅผ ๋ณด๊ฒ ๋ ๊ฒ์
๋๋ค ...
// Things to note - this is only 1 level deep - both for Arrays and Objects. lodash has a cloneDeep method, but you should think twice before using it.
// ์ฃผ๋ชฉํ ์ฌํญ-์ด๊ฒ์ ๋ฐฐ์ด๊ณผ ๊ฐ์ฒด ๋ชจ๋์ ๋ํด 1 ๋ ๋ฒจ์ ๋ถ๊ณผํฉ๋๋ค. lodash์๋ cloneDeep ๋ฉ์๋๊ฐ ์์ง๋ง ์ฌ์ฉํ๊ธฐ ์ ์ ๋ ๋ฒ ์๊ฐํด์ผํฉ๋๋ค.
</script>
</body>
</html>
// start with strings, numbers and booleans
// ๋ฌธ์์ด, ์ซ์ ๋ฐ ๋ถ์ธ๋ก ์์
let age = 100;
let age2 = age;
console.log(age, age2);
age = 200;
console.log(age, age2);
let name = 'wes';
let name2 = name;
console.log(name, name2);
name = 'wesley';
console.log(name, name2);
๋ฌธ์์ด, ์ซ์์ด๊ฒฝ์ฐ ๋ณ์์ ๋ค๋ฅธ๊ฐ์ ๋ฃ์ผ๋ฉด ๋ณ๊ฒฝ๋์ด ์ถ๋ ฅ์ด๋จ.
๊ฒฐ๊ณผ
// Let's say we have an array
// ๋ฐฐ์ด์ด ์๋ค๊ณ ๊ฐ์ ํด ๋ด
์๋ค.
const players = ['Wes', 'Sarah', 'Ryan', 'Poppy'];
// and we want to make a copy of it.
// ๋ณต์ฌ๋ณธ์ ๋ง๋ค๊ณ ์ถ์ต๋๋ค.
const team = players;
console.log(players, team);
team[3] = 'Lux';
console.log(players, team);
team[3]์ผ๋ก team์ ๋ฐฐ์ด๋ง ๋ฐ๊พธ๊ณ ์ถ์๋๋ฐ ๊ฒฐ๊ณผ๋ players๋ ํจ๊ผ ๋ฐ๋๊ฒ ๋๋ค.
ํด๊ฒฐ๋ฐฉ๋ฒ
โ slice()
const team2 = players.slice();
โก concat()
const tema3 = [].concat(players);
โข spread operator[...]
const team4 = [...players];
team4[3] = 'Heee Haaaa';
console.log(team4);
โฃ Array.from()
const team5 = Array.from(players);
์ 4๊ฐ์ง ๋ฐฉ๋ฒ์ผ๋ก ์๋ก์ด ๋ฐฐ์ด์ ๋ง๋ค์ด ๊ธฐ์กด ๋ฐฐ์ด์ ๋ณ๊ฒฝ ์์ด ๋ฐ๋ก ๊ฐ์ ๋ณ๊ฒฝํ ์ ์๋ค.
// with Objects
const person = {
name: 'Wes Bos',
age: 80
};
// and think we make a copy:
// ์ฌ๋ณธ์ ๋ง๋ ๋ค๊ณ ์๊ฐํฉ๋๋ค.
const captain = person;
captain.number = 99;
๋ฐฐ์ด๊ณผ ๋ง์ฐฌ๊ฐ์ง๋ก ์๋์ ๊ฐ์ฒด ๋ํ ๋ณ๊ฒฝ์ด ๋๋ค.
โ Object.assign( { }, ์ฐธ์กฐ ๊ฐ์ฒด, ์ถ๊ฐํ ๊ฐ)
const capt2 = Object.assign({},person, {number: 99, age: 12});console.log(capt2);
โก spread
const capt3 = {...person};
์ ๋ฐฉ๋ฒ์ผ๋ก๋ ์๋ ์ฝ๋์์ social์์ twitter๋ด์ฉ์ ๋ณ๊ฒฝํ ์ ์์ด JSON์ ์๋์ ๊ฐ์ด ์ฌ์ฉ
const wes = { name: 'Wes', age: 100, social:{ twitter: '@wesbos', facebook: 'wesbos.developer' }}console.clear();console.log(wes);const dev = Object.assign({}, wes);const dev2 = JSON.parse(JSON.stringify(wes));