Dealing With Objects in JavaScript With Object.assign, Object.keys and hasOwnProperty - Lee-hyuna/33-js-concepts-kr GitHub Wiki

์ด ๊ฒŒ์‹œ๋ฌผ์€ JavaScript์—์„œ ๊ฐ์ฒด๋ฅผ ๊ด€๋ฆฌํ•˜๋Š” ๋ฐ ์œ ์šฉํ•œ ๋ช‡ ๊ฐ€์ง€ ๋ฐฉ๋ฒ•์„ ํƒ์ƒ‰ํ•˜๋Š” ๋ฐ ๋„์›€์ด ๋˜๋Š” ์ผ์ข…์˜ Grab Bab(์ด๊ฒŒ ๋ญ”์ง€ ๋ชจ๋ฅด๊ฒ ์Œ)์ด๋‹ค. Object.keys, Object.protype.hasOwnProperty ๋ฐ ์ƒˆ๋กœ์šด Object.assign์— ๋Œ€ํ•ด ์•Œ์•„๋ณด๋„๋ก ํ•œ๋‹ค.

hasOwnProperty

hasOwnProperty๋Š” ๊ฐ์ฒด์˜ ์ธ์Šคํ„ด์Šค์— ์†์„ฑ์ด ์žˆ๋Š”์ง€ ์—†๋Š”์ง€ ํ™•์ธ ํ•  ์ˆ˜ ์žˆ๋Š” ๋ฉ”์†Œ๋“œ์ด๋‹ค. ์ด ์ ์„ ๋งค์šฐ ๋ช…ํ™•ํ•˜๊ฒŒ ์„ค๋ช…ํ•˜๋Š” ์˜ˆ์ œ๋ฅผ ์•„๋ž˜์—์„œ ํ™•์ธํ•ด๋ณด์ž.

const myObj = {
  clown: '๐Ÿคก',
  police: '๐Ÿ‘ฎ',
  santa: '๐ŸŽ…',
  farmer: '๐Ÿ‘ฉโ€๐ŸŒพ'
}

console.log('clown' in myObj); // true
console.log('valueOf' in myObj); // true

console.log(myObj.hasOwnProperty('clown')); // true
console.log(myObj.hasOwnProperty('valueOf')); // false

Object.keys

Object.keys๋Š” ๊ฐ์ฒด์˜ Property key๊ฐ’๋“ค๋งŒ ์ƒˆ๋กœ์šด ๋ฐฐ์—ด๋กœ returnํ•ด์ฃผ๋Š” ๋ฉ”์„œ๋“œ์ด๋‹ค.

const myObj = {
  clown: '๐Ÿคก',
  police: '๐Ÿ‘ฎ',
  santa: '๐ŸŽ…',
  farmer: '๐Ÿ‘ฉโ€๐ŸŒพ'
}

console.log(Object.keys(myObj));

// ["clown", "police", "santa", "farmer"]

Object.keys๋Š” for...of๋ฌธ์„ ํ†ตํ•ด ์•„์ฃผ ์œ ์šฉํ•˜๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

const myObj = {
  clown: '๐Ÿคก',
  police: '๐Ÿ‘ฎ',
  santa: '๐ŸŽ…',
  farmer: '๐Ÿ‘ฉโ€๐ŸŒพ'
}

for (let k of Object.keys(myObj)) {
  console.log(`Hey ${ myObj[k] }!`);
}

// "Hey ๐Ÿคก!"
// "Hey ๐Ÿ‘ฎ!"
// "Hey ๐ŸŽ…!"
// "Hey ๐Ÿ‘ฉโ€๐ŸŒพ!"

Note: Object.keys์—์„œ array๊ฐ€ ๋ฐ˜ํ™˜๋˜์—ˆ๋‹ค. keys๊ฐ€ ๊ผญ ์ˆœ์„œ๋Œ€๋กœ ์žˆ์„ ํ•„์š”๋Š” ์—†๋‹ค.

Object.assign

ES2015(ES6)๋Š” Object ์ƒ์„ฑ์ž์— ๋Œ€ํ•œ ์ƒˆ๋กœ์šด ์ •์  ๋ฐฉ๋ฒ•์„ ์ œ๊ณตํ•œ๋‹ค. Object.assign์€ ๊ฐ’์„ ๊ฐ์ฒด์—์„œ ๋‹ค๋ฅธ ๊ฐ์ฒด๋กœ ์‰ฝ๊ฒŒ ๋ณต์‚ฌํ•  ์ˆ˜ ์žˆ๋‹ค. ๋‹ค์Œ ์˜ˆ์ œ์—์„œ๋Š” ๋นˆ ๊ฐ์ฒด๋ฅผ ๋ฌธ์ž ๊ทธ๋Œ€๋กœ ์‚ฌ์šฉํ•˜๊ณ  myObj์˜ ์†์„ฑ์„ ๋ณต์‚ฌํ•˜์—ฌ myObj์˜ ๋ณต์‚ฌ๋ณธ์ธ ์ƒˆ ๊ฐ์ฒด(myObj3)๋ฅผ ๋งŒ๋“ ๋‹ค.

const myObj = {
  clown: '๐Ÿคก',
  police: '๐Ÿ‘ฎ',
  santa: '๐ŸŽ…',
  farmer: '๐Ÿ‘ฉโ€๐ŸŒพ'
}

const myObj2 = myObj;

const myObj3 = Object.assign({}, myObj);

console.log(Object.is(myObj, myObj2)); // true

console.log(Object.is(myObj, myObj3)); // false

console.log(myObj3);

// Object {
//   clown: "๐Ÿคก",
//   farmer: "๐Ÿ‘ฉโ€๐ŸŒพ",
//   police: "๐Ÿ‘ฎ",
//   santa: "๐ŸŽ…"
// }

Note: Object.is๋Š” ๋‘ ๊ฐ์ฒด๊ฐ€ ๋™์ผํ•œ์ง€ ํ™•์ธํ•˜๋Š” ๋ฐฉ๋ฒ•์ด๋‹ค.

Note: ๊ฐ์ฒด์˜ ์—ด๊ฑฐ ๊ฐ€๋Šฅํ•œ ์†์„ฑ๋งŒ Object.assign์œผ๋กœ ๋ณต์‚ฌ๋œ๋‹ค.

์ฒซ ๋ฒˆ์งธ ์ธ์ˆ˜๋Š” Default ๊ฐ์ฒด์ด๊ณ  ๋‹ค์Œ ์ธ์ˆ˜๋Š” ์ฃผ๊ฐ€ ๋  ๊ฐ์ฒด์ด๋‹ค. ์—ฌ๋Ÿฌ ๊ฐ์ฒด๋ฅผ ์ „๋‹ฌํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ ๋งˆ์ง€๋ง‰์œผ๋กœ ์ „๋‹ฌ๋œ ๊ฐ์ฒด์˜ ์ค‘๋ณต ์†์„ฑ์ด ๋‹ค์Œ์„ ๋”ฐ๋ฆ…๋‹ˆ๋‹ค.

const myObj = {
  clown: '๐Ÿคก',
  police: '๐Ÿ‘ฎ',
  santa: '๐ŸŽ…',
  farmer: '๐Ÿ‘ฉโ€๐ŸŒพ'
}

const myObj2 = Object.assign({}, myObj, {
  santa: '๐ŸŽ„',
  teacher: '๐Ÿ‘ฉโ€๐Ÿซ'
});

console.log(myObj2);

// Object {
//   clown: "๐Ÿคก",
//   farmer: "๐Ÿ‘ฉโ€๐ŸŒพ",
//   police: "๐Ÿ‘ฎ",
//   santa: "๐ŸŽ„",
//   teacher: "๐Ÿ‘ฉโ€๐Ÿซ"
// }

Redux์—์„œ ์‚ฌ์šฉํ•˜๋Š” ์ƒํƒœ๊ด€๋ฆฌํŒจํ„ด๊ณผ ๋น„์Šทํ•˜๋‹ค. Object.assign์„ ์‚ฌ์šฉํ•˜๋ฉด ๊ธฐ์กด ๊ฐ์ฒด์—์„œ ์™„์ „ํžˆ ์ƒˆ๋กœ์šด ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜์—ฌ, ๊ฐ์ฒด๋ฅผ ๋ณต์‚ฌํ•˜๊ณ  ํ™•์žฅํ•  ์ˆ˜ ์žˆ๋‹ค.

Bonus: Object.freeze

Object.freeze๋Š” ๊ฐ์ฒด์˜ ์›๋ณธ์ด ๋ฐ”๋€Œ์ง€ ์•Š๋„๋ก ์–•๊ฒŒ ์–ผ๋ฆฐ๋‹ค. ๋‹ค์Œ ์˜ˆ์—์„œ๋Š” ๊ฐ์ฒด์˜ ์†์„ฑ์„ ๋ณ€๊ฒฝํ•˜๊ฑฐ๋‚˜, ์ถ”๊ฐ€ํ•˜๊ฑฐ๋‚˜, ์‚ญ์ œํ•  ์ˆ˜ ์—†๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

const myObj = {
  clown: '๐Ÿคก',
  police: '๐Ÿ‘ฎ',
  santa: '๐ŸŽ…',
  farmer: '๐Ÿ‘ฉโ€๐ŸŒพ'
}

myObj.clown = 'scary';
myObj.astronaut = '๐Ÿ‘จโ€๐Ÿš€';

Object.freeze(myObj);

myObj.clown = 'really scary';
myObj.student = '๐Ÿ‘ฉโ€๐ŸŽ“';
delete myObj.santa;

console.log(myObj);

// Object {
//   clown: "scary",
//   farmer: "๐Ÿ‘ฉโ€๐ŸŒพ",
//   police: "๐Ÿ‘ฎ",
//   santa: "๐ŸŽ…",
//   astronaut: "๐Ÿ‘จโ€๐Ÿš€"
// }

๊ทธ๋ฆฌ๊ณ  ๋˜ ๋‹ค๋ฅธ ๋ฉ”์„œ๋“œ์ธ Object.isFrozen๋ฅผ ์‚ฌ์šฉํ•˜๋Š”๋ฐ, Object๊ฐ€ frozen์ด ๋˜์—ˆ๋Š”์ง€ ์ฒดํฌํ•˜๋Š” ๋ฉ”์„œ๋“œ์ด๋‹ค.

const myObj = {
  clown: '๐Ÿคก',
  police: '๐Ÿ‘ฎ',
  santa: '๐ŸŽ…',
  farmer: '๐Ÿ‘ฉโ€๐ŸŒพ'
}

console.log(Object.isFrozen(myObj)); // false

Object.freeze(myObj);

console.log(Object.isFrozen(myObj)); // true

์ค‘์ฒฉ๋œ ๊ฐ์ฒด๋Š” ๊ฐ์ฒด์— ์˜ํ•ด ์ž๋™์œผ๋กœ ๊ณ ์ •๋˜์ง€ ์•Š๋Š”๋‹ค. ๋‹ค์Œ ์˜ˆ์ œ์—์„œ๋Š” ๊ฐ์ฒด๋ฅผ ์–ผ๋ฆฐ ํ›„์—๋„ ์ค‘์ฒฉ๋œ animals๊ฐ์ฒด์˜ ์†์„ฑ์ด ๋ณ€๊ฒฝ๋˜๊ฑฐ๋‚˜ ์‚ญ์ œ๋  ์ˆ˜ ์žˆ๋‹ค.

const myObj = {
  clown: '๐Ÿคก',
  police: '๐Ÿ‘ฎ',
  santa: '๐ŸŽ…',
  farmer: '๐Ÿ‘ฉโ€๐ŸŒพ',
  animals: {
    cow: '๐Ÿ„',
    rabbit: '๐Ÿ‡'
  }
}

Object.freeze(myObj);

delete myObj.animals.rabbit;
myObj.animals.cow = 'moo!';

console.log(myObj);

// Object {
//   clown: "๐Ÿคก",
//   farmer: "๐Ÿ‘ฉโ€๐ŸŒพ",
//   police: "๐Ÿ‘ฎ",
//   santa: "๐ŸŽ…",
//   animals: {
//     cow: 'moo!'
//   }
// }