[JavaScript30] ๐Ÿ“„ 14. JavaScript References VS Copying

์กฐ์ค€ํ˜•ยท2021๋…„ 7์›” 10์ผ
0

JavaScript30

๋ชฉ๋ก ๋ณด๊ธฐ
14/30

๐Ÿ“„ 14. JavaScript References VS Copying

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>

๐ŸŒ ์ฝ”๋“œ ๋ถ„์„

๐Ÿ‘‰ 1. ๋ฌธ์ž์—ด, ์ˆซ์ž์—ด, ๋ถˆ๋ฆฐ์ผ ๋•Œ ๋ณ€์ˆ˜ ์„ ์–ธ ๋ฐ ๋ณ€๊ฒฝ

// 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);

๋ฌธ์ž์—ด, ์ˆซ์ž์—ด๊ฒฝ์šฐ ๋ณ€์ˆ˜์— ๋‹ค๋ฅธ๊ฐ’์„ ๋„ฃ์œผ๋ฉด ๋ณ€๊ฒฝ๋˜์–ด ์ถœ๋ ฅ์ด๋จ.

๊ฒฐ๊ณผ

๐Ÿ‘‰ 2. ๋ฐฐ์—ด์ผ ๋•Œ ๋ณ€์ˆ˜ ์„ ์–ธ ๋ฐ ๋ณ€๊ฒฝ

// 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๊ฐ€์ง€ ๋ฐฉ๋ฒ•์œผ๋กœ ์ƒˆ๋กœ์šด ๋ฐฐ์—ด์„ ๋งŒ๋“ค์–ด ๊ธฐ์กด ๋ฐฐ์—ด์˜ ๋ณ€๊ฒฝ ์—†์ด ๋”ฐ๋กœ ๊ฐ’์„ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ๋‹ค.

๐Ÿ‘‰ 3. ๊ฐ์ฒด์ผ ๋•Œ ๋ณ€์ˆ˜ ์„ ์–ธ ๋ฐ ๋ณ€๊ฒฝ

// 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));

profile
๊นƒํ—ˆ๋ธŒ : github.com/JuneHyung

0๊ฐœ์˜ ๋Œ“๊ธ€