Object and Arrays - Reference VS Copy

Start with strings, numbers and booleans

let age = 100;
let age2 = age;
console.log(age, age2); //100, 100

age = 200;
console.log(age, age2); //200, 100

age2์— age๋ฅผ ํ• ๋‹นํ•˜๊ณ  age๋ฅผ ๋ฐ”๊ฟ”์ง€๋งŒ age2์˜ ๊ฐ’์€ ๋ณ€ํ•˜์ง€ ์•Š๋Š”๋‹ค. ์ด๊ฒƒ์€ primitive์ด๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

Reference - array

 // 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); // ['Wes', 'Sarah', 'Ryan', 'Poppy'] x2
// You might think we can just do something like this:
team[3] = 'Lux';

console.log(players, team); // ['Wes', 'Sarah', 'Ryan', 'Lux'] x2

๋‚˜๋Š” team์„ ๋ณ€๊ฒฝํ–ˆ์ง€๋งŒ, players๋„ ์˜ํ–ฅ์„ ๋ฐ›๋Š”๋‹ค. ๋‚ด๊ฐ€ ์˜ค๋ฆฌ์ง€๋„์ด๋‚˜ ๋‹ค๋ฅธ array๋ฅผ ๋ณ€๊ฒฝํ•˜๋ฉด ๊ทธ๊ฒƒ์€ reference๋กœ ๋˜๋Œ์•„ ๊ฐ€๋ ค๊ณ  ํ•œ๋‹ค.

Array copy

const players = ['Wes', 'Sarah', 'Ryan', 'Poppy'];

// copy ๋ฐฉ๋ฒ• 1
const team1 = players.slice();

// copy ๋ฐฉ๋ฒ• 2
const team2 = [].concat(players);
team2[2] = 'Lux';

// copy ๋ฐฉ๋ฒ• 3 - es6 spread
const team3 = [...players];

// copy ๋ฐฉ๋ฒ• 4 
const team4 = Array.from(players);

console.log(team2) // ['Wes', 'Sarah', 'Ryan', 'Lux'];
console.log(players) // ['Wes', 'Sarah', 'Ryan', 'Poppy'];

referenceํ•˜๊ณ  ์‹ถ์ง€์•Š๊ณ  copy๊ฐ€ ๋ชฉํ‘œ๋ผ๋ฉด slice, concat, spread, Array.from ์ด 4๊ฐ€์ง€๋ฅผ ์ด์šฉํ•˜์ž.

With Objects

const person = {
    name: 'Wes Bos',
    age: 80
};

const captain = person;
captain.number = 99;

console.log(person) // {name: 'Wes Bos',age: 80, number: 99}

Object copy

const person = {
    name: 'Wes Bos',
    age: 80
};

// copy ๋ฐฉ๋ฒ• 1 - 3๋ฒˆ์งธ ์ธ์ž๋Š” ์ƒˆ๋กœ ์ถ”๊ฐ€, ์ˆ˜์ •ํ•  key, value์ด๋‹ค. 
const cap1 = Object.assign({}, person, { number: 99, age: 12 });

// copy ๋ฐฉ๋ฒ• 2 
const cap2 = {...person};

Object - one level deep

const wes = {
    name: 'Wes',
    age: 100,
    social: {
        twitter: '@wesbos',
        facebook: 'wesbos.developer'
    }
}

 const dev = {...wes};
dev.social.twitter = '@coolman';

console.log(dev.social); // {twitter: "@coolman", facebook: "wesbos.developer"}

console.log(wes.social); // {twitter: "@coolman", facebook: "wesbos.developer"}

one level deep์— ์žˆ๋Š” ๋‚ด์šฉ์€ reference๋ผ wes.social๋ฅผ ์ฐธ์กฐํ•œ๋‹ค. wes๋Š” ์ด ๋‚ด์šฉ์„ shit์ด๋ผ ํ‘œํ˜„ํ–ˆ๋‹ค.

์ด๋Ÿฐ์‹์˜copy๋Š” ์˜ค์ง one level deep๋งŒ ๊ฐ€๋Šฅํ•˜๋‹ค.

๋ชจ๋“  level deep์„ ํ•˜๊ณ ์ž ํ•  ๊ฒฝ์šฐ ๋จผ์ € ์Šค์Šค๋กœ์—๊ฒŒ ๋ฌผ์–ด์•ผ ํ•œ๋‹ค. ์ด๊ฒŒ ์ •๋ง ํ•„์š”ํ•œ๊ฐ€?

ํ•˜์ง€๋งŒ ์ •๋ง every level deep์ด ํ•„์š”ํ•˜๋‹ค๋ฉด? (์ถ”์ฒœx)

const wes = {
    name: 'Wes',
    age: 100,
    social: {
        twitter: '@wesbos',
        facebook: 'wesbos.developer'
    }
}

const dev2 = JSON.parse(JSON.stringify(wes));

dev2.social.twitter = '@coolman';

// ์˜ค๋ฆฌ์ง€๋„ ๊ฐ์ฒด์—๋Š” ํ„ฐ์น˜๊ฐ€ ์—†๋‹ค.
console.log(wes.social);// {twitter: "@wesbos", facebook: "wesbos.developer"}

/*
* JSON์€ string์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
* JSON.stringify(wes)
* ํ•˜์ง€๋งŒ  JSON.parse์„ ์‚ฌ์šฉํ•˜์—ฌ ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•˜์˜€๋‹ค.
*/

์ด๋ฅผ .. poor man's deep clone ์ด๋ผ ๋ถˆ๋ฆฐ๋‹ค.'ใ…'... ํ—‰

Last updated

Was this helpful?