๐Ÿฅ‚ ์ฝ”์–ด์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ 1์žฅ part2 (์–•์€๋ณต์‚ฌ vs ๊นŠ์€ ๋ณต์‚ฌ)

KHWยท2021๋…„ 2์›” 16์ผ
1
post-custom-banner

์–•์€๋ณต์‚ฌ vs ๊นŠ์€๋ณต์‚ฌ

์–•์€๋ณต์‚ฌ : ๋ฐ”๋กœ ์•„๋ž˜ ๋‹จ๊ณ„์˜ ๊ฐ’๋งŒ ๋ณต์‚ฌ
๊นŠ์€๋ณต์‚ฌ : ๋‚ด๋ถ€์˜ ๋ชจ๋“  ๊ฐ’๋“ค์„ ํ•˜๋‚˜ํ•˜๋‚˜ ์ฐพ์•„์„œ ์ „๋ถ€ ๋ณต์‚ฌ
(๊ฐ์ฒด์•ˆ์˜ ๊ฐ์ฒด๋‚ด์šฉ๋งˆ์ € ์ „๋ถ€ ํ•˜๋‚˜ํ•˜๋‚˜ ๋ณต์‚ฌํ•œ๋‹ค)

์–•์€ ๋ณต์‚ฌ์˜ ๋ฌธ์ œ์ 

์ค‘์ฒฉ๋œ ๊ฐ์ฒด์— ๋Œ€ํ•ด์„œ ์•„๋ž˜ ๋‹จ๊ณ„์˜ ๋ณต์‚ฌ๋งŒ ์ด๋ค„์งˆ ๊ฒฝ์šฐ ํ•ด๋‹น ์•„๋ž˜์˜ ํ”„๋กœํผํ‹ฐ ๋ณ€๊ฒฝ์—์„œ ๋‹ค๋ฅธ ๊ฐ์ฒด์™€ ์„œ๋กœ ์˜ํ–ฅ์„ ๋ฐ›๋Š”๋‹ค. (๋ถˆ๋ณ€ ๊ฐ์ฒด๊ฐ€ ์•„๋‹ˆ๊ฒŒ ๋œ๋‹ค.)

var copyObject = function (target){
 var result = {};
 for( var prop in target){
   result[prop] = target[prop];
 }
 return result;
};

var user = {
 name : 'msk',
 information : {
   friend : '์ฟ ๋กœ์ฝ”',
   school : 'ํ† ํ‚ค์™€๋‹ค์ด'
 }
}

var user2 = copyObject(user);

user2.name = 'kim';
console.log(user.name === user2.name); // false

user.information.school = '๋‹จ๊ตญ๋Œ€ํ•™๊ต'
console.log(user.information.school === user2.information.school) //true

์–•์€๋ณต์‚ฌ๋กœ ์–•์€๋ถ€๋ถ„์ธ name์—๋Š” ์„œ๋กœ ์˜ํ–ฅ์„ ๋ผ์น˜์ง€ ์•Š์•˜์ง€๋งŒ ๊นŠ์€๋ถ€๋ถ„์ธ school์—๋Š” ์„œ๋กœ ์˜ํ–ฅ์„ ๋ผ์นœ๋‹ค.

๋”ฐ๋ผ์„œ ๊ธฐ๋ณธํ˜• ๋ฐ์ดํ„ฐ์ผ ๊ฒฝ์šฐ์—๋Š” ๊ทธ๋Œ€๋กœ ๋ณต์‚ฌํ•˜๋ฉด ๋˜์ง€๋งŒ ์ฐธ์กฐํ˜• ๋ฐ์ดํ„ฐ๋Š” ๋‹ค์‹œ ๊ทธ ๋‚ด๋ถ€์˜ ํ”„๋กœํผํ‹ฐ๋“ค์„ ๋ณต์‚ฌํ•ด์•ผํ•œ๋‹ค.

๊นŠ์€๋ณต์‚ฌ ๋ฐฉ๋ฒ•

  1. ์žฌ๊ท€ํ•จ์ˆ˜ : ๋‚ด๋ถ€ ํ”„๋กœํผํ‹ฐ๊นŒ์ง€์ ์šฉ
  2. ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ํ•จ์ˆ˜
  3. JSON ๋ฌธ์ž์—ด ๋ณ€ํ™˜ ๋ฐ ๊ฐ์ฒด๋กœ ๋‹ค์‹œ ๋ณ€ํ™˜ : ๊ฐ์ฒด์˜ ์ฐธ์กฐ ์‚ญ์ œ ์˜ํ–ฅ
  4. ๋ฆฌํ„ดํ•œ ๊ฐ์ฒด ํ˜น์€ ๋ฐฐ์—ด ์‚ฌ์šฉํ•˜๊ธฐ
  5. [...] ์‚ฌ์šฉ

์žฌ๊ท€ํ•จ์ˆ˜

var copyObjectDeep = function (target){
  var result = {};
  if(typeof target === 'object' && target !== null){
  	for( var prop in target){
    	result[prop] = copyObjectDeep(target[prop]);
  	}
  }
  else{
  	result = target
  }
  return result;
};

์ด์ „์˜ ํ•จ์ˆ˜์™€ ๋‹ค๋ฅด๊ฒŒ ์žฌ๊ท€๋กœ ๋‚ด๋ถ€๊ฐ€ ๊ฐ์ฒด๋ผ๋ฉด ๋‚ด์šฉ์„ ๋ฐ˜๋ณตํ•ด์„œ ๋ณต์‚ฌํ•˜๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค.

Json ์‚ฌ์šฉ๋ฒ•

var copyObjectViaJson = function (target){
  return JSON.parse(JSON.stringify(target));
}

๋ฌธ์ž์—ด๋กœ ๋ณ€ํ™˜ ํ›„ ๋‹ค์‹œ ๊ฐ์ฒด๋กœ ๋ณ€ํ™˜ํ•˜๋ฉด ๊ฐ์ฒด์— ๋Œ€ํ•œ ์ฐธ์กฐ๊ฐ€ ์—†์–ด์ง„๋‹ค => ์„œ๋กœ ์˜ํ–ฅ์„ ๋ฐ›์ง€์•Š๋Š”๋‹ค.

๊ตฌ์กฐ๋ถ„ํ•ดํ• ๋‹น

const a = [1,2,3]
const b = [...a]

a.splice(0,1)
a
[2, 3]
b
[1, 2, 3]

undefined vs null

๊ธฐ๋ณธ์ ์œผ๋กœ ์„œ๋กœ๋ฅผ ๋น„๊ตํ•˜๋ ค๋ฉด ===(์ผ์น˜์—ฐ์‚ฐ์ž)๋ฅผ ์จ์•ผํ•œ๋‹ค.

console.log(null == undefined) // true

null : ๋ช…์‹œ์ ์œผ๋กœ ๋น„์–ด์žˆ์Œ์„ ๋‚˜ํƒ€๋‚ด๊ณ  ์‹ถ์„ ๋•Œ
undefined : ๊ฐ’์„ ๋Œ€์ž…ํ•˜์ง€์•Š์€ ๋ณ€์ˆ˜์— ์ ‘๊ทผํ•˜๊ณ ์ž ํ•  ๋•Œ

undefined vs empty(๋น„์–ด์žˆ๋Š” ์š”์†Œ)

undefinedempty
์ˆœํšŒ(forEach)์ž‘๋™๋ฌด์‹œ
ํ”„๋กœํผํ‹ฐ, ํ‚ค ๊ฐ’์กด์žฌ์กด์žฌX

ํ—ท๊ฐˆ๋ฆฌ๋‹ˆ๊นŒ ๊ทธ๋ƒฅ ๋น„์–ด์žˆ์Œ์„ ๋ช…์‹œ์ ์œผ๋กœ ๋‚˜ํƒ€๋‚ด๊ณ  ์‹ถ์„๋•Œ๋Š” null์„ ์‚ฌ์šฉํ•ด๋ผ

์ •๋ฆฌ

  1. ๊ธฐ๋ณธํ˜•(๋ถˆ๋ณ€์„ฑ) vs ์ฐธ์กฐํ˜•(๊ฐ€๋ณ€์„ฑ)
  2. ๋ณ€์ˆ˜์™€ ์‹๋ณ„์ž
    ๋ณ€์ˆ˜ : ๋ณ€๊ฒฝ ๊ฐ€๋Šฅํ•œ ๋ฐ์ดํ„ฐ๊ฐ€ ๋‹ด๊ธธ ์ˆ˜ ์žˆ๋Š” ๊ณต๊ฐ•
    ์‹๋ณ„์ž : ๊ทธ ๋ณ€์ˆ˜์˜ ์ด๋ฆ„
  3. ์–•์€๋ณต์‚ฌ vs ๊นŠ์€๋ณต์‚ฌ
  4. ๊นŠ์€ ๋ณต์‚ฌ์˜ 3๊ฐ€์ง€ ๋ฐฉ๋ฒ•
  5. undefined vs null

[...๋ฐฐ์—ด] , {...๊ฐ์ฒด} ๋Š” ์–•์€๋ณต์‚ฌ์ผ๊นŒ? ๊นŠ์€ ๋ณต์‚ฌ์ผ๊นŒ?

๋ฐฐ์—ด

[...] ์‚ฌ์šฉํ•˜๊ธฐ

const arr = [
  {name:[1,2,3,4]},{age:[10,20,30,40]}
]

const arrCopy = [...arr]

arrCopy[0].name[1] = 100

console.log(arr,arrCopy)

const arr1 = [1,2,3,4,5]

const arr1Copy = [...arr1]

arr1Copy[0] = 1000

console.log(arr1,arr1Copy)

// { name: [ 1, 100, 3, 4 ] }, { age: [ 10, 20, 30, 40 ] } ] [ { name: [ 1, 100, 3, 4 ] }, { age: [ 10, 20, 30, 40 ] } ]
//   [ 1, 2, 3, 4, 5 ] [ 1000, 2, 3, 4, 5 ]

arrCopy์˜ ๋‚ด๋ถ€๊ฐ’์„ 100์œผ๋กœ ๋ฐ”๊พธ์—ˆ์œผ๋‚˜
arr์˜ ๋‚ด๋ถ€๊ฐ’ ๋˜ํ•œ 100์œผ๋กœ ๋ฐ”๊พธ์–ด์กŒ๋‹ค.

๊นŠ์ด๊ฐ€ 1์ธ arr1๊ณผ arr1copy๋Š” arr1Copy์˜ ๋ณ€์ˆ˜๊ฐ’์ด ๋ฐ”๋€Œ์–ด๋„ arr1์€ ๋ฐ”๋€Œ์ง€์•Š์€ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

JSON ์‚ฌ์šฉํ•˜๊ธฐ

const arr = [
  {name:[1,2,3,4]},{age:[10,20,30,40]}
]

// const arrCopy = [...arr]
const arrCopy = JSON.parse(JSON.stringify(arr))

arrCopy[0].name[1] = 100

console.log(arr,arrCopy)

const arr1 = [1,2,3,4,5]

// const arr1Copy = [...arr1]
const arr1Copy = JSON.parse(JSON.stringify(arr1))

arr1Copy[0] = 1000

console.log(arr1,arr1Copy)

  // [ { name: [ 1, 2, 3, 4 ] }, { age: [ 10, 20, 30, 40 ] }]
  // [ { name: [ 1, 100, 3, 4 ] }, { age: [ 10, 20, 30, 40 ] } ]
  // [ 1, 2, 3, 4, 5 ] [ 1000, 2, 3, 4, 5 ]

arrCopy์˜ ๋‚ด๋ถ€ ๊ฐ’์ด ๋ฐ”๋€Œ์–ด๋„ arr์˜ ๋‚ด๋ถ€๊ฐ’์—๋Š” ์˜ํ–ฅ์„ ์ฃผ์ง€์•Š๋Š”๋‹ค.
arr1Copy๋Š” ๊นŠ์ด๊ฐ€ 1๋กœ ๋‚ด๋ถ€๊ฐ’์ด ๋ฐ”๋€Œ์–ด๋„ arr1์˜ ๋‚ด๋ถ€๊ฐ’์—๋Š” ์˜ํ–ฅ์„ ์ฃผ์ง€์•Š๋Š”๋‹ค.

๊ฐ์ฒด

{...}์‚ฌ์šฉํ•˜๊ธฐ

const user = {
  name : 'msk',
  information : {
    friend : '์ฟ ๋กœ์ฝ”',
    school : 'ํ† ํ‚ค์™€๋‹ค์ด'
  }
}


const user1 = {...user}
user.information.friend = '๋ฐ”๋€Œ์–ด๋ผ'
user1.name = '~~~'
console.log(user,user1)

//{ name: 'msk', information: { friend: '๋ฐ”๋€Œ์–ด๋ผ', school: 'ํ† ํ‚ค์™€๋‹ค์ด' } } 
//{ name: '~~~', information: { friend: '๋ฐ”๋€Œ์–ด๋ผ', school: 'ํ† ํ‚ค์™€๋‹ค์ด' } }

user.information.friend์˜ ๊นŠ์ด 2์˜ ๋ถ€๋ถ„์˜ ๋ฐ”๋€œ์ด user1์—๋„ ์˜ํ–ฅ์„ ๋ผ์นœ๋‹ค.

JSON ์‚ฌ์šฉํ•˜๊ธฐ

const user = {
  name : 'msk',
  information : {
    friend : '์ฟ ๋กœ์ฝ”',
    school : 'ํ† ํ‚ค์™€๋‹ค์ด'
  }
}


// const user1 = {...user}
// user.information.friend = '๋ฐ”๋€Œ์–ด๋ผ'
// user1.name = '~~~'
// console.log(user,user1)

const user1 = JSON.parse(JSON.stringify(user))
user.information.friend = '๋ฐ”๋€Œ์–ด๋ผ'
user1.name = '~~~'
console.log(user,user1)
//{ name: 'msk', information: { friend: '๋ฐ”๋€Œ์–ด๋ผ', school: 'ํ† ํ‚ค์™€๋‹ค์ด' } } 
//{ name: '~~~', information: { friend: '์ฟ ๋กœ์ฝ”', school: 'ํ† ํ‚ค์™€๋‹ค์ด' } }

user.information.friend๊ฐ€ user1์—๋Š” ์˜ํ–ฅ์„ ๋ผ์น˜์ง€์•Š๋Š”๋‹ค.

๋ฆฌํ„ดํ•œ ๊ฐ์ฒด์™€ ๊ทธ๋ƒฅ ๊ฐ์ฒด

const obj = {
  count : 0
}

const objFunc = ()=>{
  return {count:0}
}

const A = obj
const B = obj
const C = objFunc()
const D = objFunc()

console.log(A,B,C,D)

A.count = 1000
C.count = 1000000

console.log(A,B,C,D)

//{ count: 0 } { count: 0 } { count: 0 } { count: 0 }
//{ count: 1000 } { count: 1000 } { count: 1000000 } { count: 0 }

๊ทธ๋ƒฅ ๊ฐ์ฒด๋ฅผ ๋ณต์‚ฌํ•˜์—ฌ ์‚ฌ์šฉํ•œ ๊ฒฝ์šฐ A์˜ ๊ฐ’์ด ๋ฐ”๋€Œ๋ฉด B์˜ ๊ฐ’์— ์˜ํ–ฅ์„ ์ค€๋‹ค.
ํ•˜์ง€๋งŒ returnํ•œ ๊ฐ์ฒด๊ฐ€ ๋ณต์‚ฌ๋˜์–ด ์‚ฌ์šฉํ•œ ๊ฒฝ์šฐ C๊ฐ€ ๋ฐ”๋€Œ์–ด๋„ D๊ฐ€ ๋ฐ”๋€Œ์ง€์•Š๋Š”๋‹ค.

์ •๋ฆฌ

  • ์–•์€๋ณต์‚ฌ
  1. ๊ฐ’์„ ๋ฐ”๋กœ ๋ฐ›๊ธฐ
  2. [...] , {...}์‚ฌ์šฉํ•˜๊ธฐ
  • ๊นŠ์€๋ณต์‚ฌ
  1. ์žฌ๊ท€ํ•จ์ˆ˜ : ๋‚ด๋ถ€ ํ”„๋กœํผํ‹ฐ๊นŒ์ง€์ ์šฉ
  2. ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ํ•จ์ˆ˜
  3. JSON ๋ฌธ์ž์—ด ๋ณ€ํ™˜ ๋ฐ ๊ฐ์ฒด๋กœ ๋‹ค์‹œ ๋ณ€ํ™˜ : ๊ฐ์ฒด์˜ ์ฐธ์กฐ ์‚ญ์ œ ์˜ํ–ฅ
  4. ๋ฆฌํ„ดํ•œ ๊ฐ์ฒด ํ˜น์€ ๋ฐฐ์—ด ์‚ฌ์šฉํ•˜๊ธฐ

์ถœ์ฒ˜

์ฝ”์–ด์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ

profile
๋‚˜์˜ ํ•˜๋ฃจ๋ฅผ ๊ฐ€๋Šฅํ•œ ๊ธฐ์–ตํ•˜๊ณ  ์ฆ๊ธฐ๊ณ  ํ›„ํšŒํ•˜์ง€๋ง์ž
post-custom-banner

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