๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๊ฐœ๋ฐœ_TIL

๊ฐœ๋ฐœ_TIL | 2022-07-27 (69)

by Hee94 2022. 7. 27.

๐Ÿ“Œ Object


์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ 7. ์˜ค๋ธŒ์ ํŠธ ๋„Œ ๋ญ๋‹ˆ? | ํ”„๋ก ํŠธ์—”๋“œ ๊ฐœ๋ฐœ์ž ์ž…๋ฌธํŽธ (JavaScript ES6)

Object๋ž€?

  • primitive ํƒ€์ž…(number, str …)์€ ํ•˜๋‚˜์˜ ๋ณ€์ˆ˜์— ํ•˜๋‚˜์˜ ๊ฐ’๋งŒ ํ• ๋‹นํ•  ์ˆ˜ ์žˆ๋‹ค
  • ์ถœ๋ ฅํ•˜๋ ค๋ฉด ๊ฐ’์„ ๊ฐ๊ฐ ๋‹ค ์ ์–ด์ค˜์•ผ ํ•œ๋‹ค
  • ํ•จ์ˆ˜์˜ ์ธ์ž๋กœ ๋„ฃ์„ ๋•Œ๋„ ๊ฐ๊ฐ ๋‹ค ์ ์–ด์ค˜์•ผ ํ•œ๋‹ค
  • primitve ํƒ€์ž…์˜ ๋ณ€์ˆ˜๊ฐ€ ๋„ˆ๋ฌด ๋งŽ์•„์ง€๋ฉด ๊ด€๋ฆฌํ•˜๊ธฐ๋„ ํž˜๋“ค๊ณ , ๋กœ์ง€์ปฌํ•˜๊ฒŒ ์ƒ๊ฐํ•˜๊ธฐ๋„ ์–ด๋ ต๋‹ค
  • ๋”ฐ๋ผ์„œ ์˜ค๋ธŒ์ ํŠธ ํ˜•ํƒœ๋กœ ์ž‘์„ฑํ•ด์„œ ๊ฐ„ํŽธํ•˜๊ฒŒ ๋ฐ์ดํ„ฐ๋ฅผ ๊ด€๋ฆฌํ•˜์ž

Literals and Properties

  1. object literal
    • const obj = { }; ๊ณผ ๊ฐ™์ด ์ค‘๊ด„ํ˜ธ๋ฅผ ์—ด์–ด์„œ ๋งŒ๋“ค๊ธฐ
    • const ellie = {name:'ellie', age:4}; // {key : value} ellie.hasJob = true; // ์˜ค๋ธŒ์ ํŠธ ์„ ์–ธ ์ด ํ›„์˜ ๋ฐธ๋ฅ˜ ์ƒ์„ฑ delete ellie.hasJob; // ์˜ค๋ธŒ์ ํŠธ ์„ ์–ธ ์ด ํ›„์˜ ๋ฐธ๋ฅ˜ ์‚ญ์ œ
    • ํŠน์ดํ•˜๊ฒŒ ์„ ์–ธ์„ ํ•œ ์ดํ›„์— ๊ฐ’์„ ์ถ”๊ฐ€ํ•  ์ˆ˜๊ฐ€ ์žˆ๋‹ค.
    • Javascript๊ฐ€ ๋™์ ์œผ๋กœ ํƒ€์ž…์„ ์ธ์ง€ํ•˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค
      • ๊ทธ๋Ÿฌ๋‚˜ ๋„ˆ๋ฌด ๋งŽ์ด ์‚ฌ์šฉํ•˜๊ธฐ์—๋Š” ๋ฏธ์นœ์ง“์ด๋‹ค!
    • ํŠน์ดํ•˜๊ฒŒ ์„ ์–ธ์„ ํ•œ ์ดํ›„์— ๊ฐ’์„ ์‚ญ์ œํ•  ์ˆ˜๊ฐ€ ์žˆ๋‹ค
  2. object constructor
    • const obj = new Object(); ์™€ ๊ฐ™์ด new๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๋งŒ๋“ค๊ธฐ

Computed Property

  • ์˜ค๋ธŒ์ ํŠธ๋Š” ๋ณ€์ˆ˜.ํ”„๋กœํผํ‹ฐ ํ˜น์€ ๋ณ€์ˆ˜[’ํ”„๋กœํผํ‹ฐ’] ์ŠคํŠธ๋ง ํ˜•์‹์œผ๋กœ ํ”„๋กœํผํ‹ฐ์— ์ ‘๊ทผํ•  ์ˆ˜์žˆ๋‹ค. ํ›„์ž์˜ ๊ฒฝ์šฐ Computed Property์— ํ•ด๋‹น๋œ๋‹ค
    • key๋Š” ํ•ญ์ƒ string ํƒ€์ž…์œผ๋กœ ์ž‘์„ฑํ•ด์•ผ ํ•œ๋‹ค
  • ์ผ๋ฐ˜์ ์œผ๋กœ ์ฝ”๋”ฉํ•  ๋•Œ๋Š” dot(.) ์„์‚ฌ์šฉํ•˜์—ฌ ์ž‘์„ฑํ•˜์ง€๋งŒ, ์ •ํ™•ํ•˜๊ฒŒ ๋ฐธ๋ฅ˜์˜ ํƒ€์ž…์„ ๋ชฐ๋ผ์„œ ๋ฉ”์„œ๋“œ๋ฅผ ์ž‘์„ฑํ•˜์ง€ ๋ชป ํ•  ๋•Œ๋Š” ๋Œ€๊ด„ํ˜ธ([ ]) ๋ฐฉ์‹์˜ ์ปดํ“จํŠธ ํ”„๋กœํผํ‹ฐ๋กœ ์ž‘์„ฑํ•œ๋‹ค
    • ์ฝ”๋”ฉํ•  ๋•Œ๋Š” ์  ํ•˜๋‚˜ ์ฐ๋Š”๊ฒŒ ํŽธํ•˜๋‹ˆ๊นŒ! ๋˜, ๋ฉ”์„œ๋“œ๋ฅผ ์ž์—ฐ์Šค๋Ÿฝ๊ฒŒ ๋’ค์— ๋ถ™์—ฌ์„œ ์ž‘์„ฑํ•˜๊ธฐ ์ข‹๋‹ค!
    • ๋ฐ˜๋ฉด ์ปดํ“จํŠธ ํ”„๋กœํผํ‹ฐ์˜ ๊ฒฝ์šฐ, ๊ฐ’์ด ๋ฌด์—‡์ด๋“  ์ƒ๊ด€์—†๊ฑฐ๋‚˜ ๊ฐ’์ด ๋ญ๊ฐ€ ๋“ค์–ด์˜ฌ ์ง€ ์•„์˜ˆ ๋ชจ๋ฅด๋Š” ๊ฒฝ์šฐ, ๋‹จ์ˆœ ๊ฒ€์‚ฌ์‹์— ์‚ฌ์šฉํ•˜๊ฑฐ๋‚˜ ํ•  ๋•Œ๋Š” ์ ์–ด์ฃผ๊ธฐ๋„ ํ•œ๋‹ค
    • function printValue(obj, key) { // console.log(obj.key); ๋ฅผ ์ด ๊ฒฝ์šฐ์—๋Š” ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๋‹ค console.log(obj[key]); } printValue(ellie, 'name'); printValue(ellie, 'age');

Property Value Shorthand

  • ์˜ค๋ธŒ์ ํŠธ๋ฅผ ์ผ์ผ์ด key : value ๋กœ ๋งŒ๋“ค์–ด์ฃผ๊ธฐ์—๋Š” ํŠน์ˆ˜๋ฌธ์ž๊ฐ€ ๋ฒˆ๊ฑฐ๋กญ๋‹ค
  • function์„ ์‚ฌ์šฉํ•˜์—ฌ ์˜ค๋ธŒ์ ํŠธ๋กœ ์„ ์–ธํ•ด์ฃผ๋Š” ๋ฐฉ์‹์ด ์กฐ๊ธˆ ๋” ํŽธ๋ฆฌํ•˜๋‹ค! → ์ด๊ฒŒ Shorthand
// ์ผ๋ฐ˜์ ์ธ ์˜ค๋ธŒ์ ํŠธ์˜ ์„ ์–ธ๋ฐฉ๋ฒ•
const person1 = {name: 'bob', age: 2};
const person2 = {name: 'steve', age: 3};
const person3 = {name: 'dave', age: 4};

// Shorthand
const person4 = makePerson('ellie', 30);
console.log(person4);
function makePerson(name, age) {
    return {
        name,
        age,
    };
}

Constructor Function

  • ์˜ค๋ธŒ์ ํŠธ๋ฅผ ์ผ์ผ์ด ๋งŒ๋“ค๋•Œ๋งˆ๋‹ค, ํ‚ค๋ฐธ๋ฅ˜๋ฅผ ์ ๋Š”๊ฒŒ ๊ท€์ฐฎ๋‹ค
  • ๊ทธ๋ž˜์„œ ํ•จ์ˆ˜๋ฅผ ๋งˆ์น˜ ํด๋ž˜์Šค์ฒ˜๋Ÿผ ๋งŒ๋“ค์–ด์„œ ์˜ค๋ธŒ์ ํŠธ๋ฅผ ์„ ์–ธํ•˜๋Š” ๋ฐฉ์‹์ด Contructor Function
  • ํด๋ž˜์Šค ์„ ์–ธ ๊ทœ์น™์ฒ˜๋Ÿผ function ๋Œ€๋ฌธ์ž๋กœ ์‹œ์ž‘ํ•˜๋Š” ํ•จ์ˆ˜์ด๋ฆ„ (ํ•„๋“œ){} ์™€ ๊ฐ™์€ ๋ฐฉ์‹์œผ๋กœ ์„ ์–ธํ•˜๋ฉฐ, ์ด ํ•จ์ˆ˜์ž์ฒด๊ฐ€ constructor ์—ญํ• ์„ ์ˆ˜ํ–‰ํ•œ๋‹ค๊ณ  ํ•˜์—ฌ constructor function ์ด๋ผ๊ณ  ๋ถ€๋ฅธ๋‹ค
const person4 = makePerson('ellie', 30);
console.log(person4);
function Person(name, age) {
        // this = {};
        this.name = name;
        this.age = age;
        // return this;
}

In Operator

  • key๊ฐ€ object์•ˆ์— ์กด์žฌํ•˜๋Š”์ง€ ํ™•์ธํ•˜๋Š” ๋ฉ”์„œ๋“œ
  • key in object ํ˜•์‹์œผ๋กœ ์ž‘์„ฑํ•œ๋‹ค
console.log('name' in ellie);

for in & for of

  • ์˜ค๋ธŒ์ ํŠธ ๋‚ด๋ถ€์˜ ํ‚ค, ๋ฐธ๋ฅ˜๋ฅผ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉํ•˜๋Š” ๋ฉ”์„œ๋“œ
  1. for (key in obj)
    • for (key in object) {} ํ˜•์‹์œผ๋กœ ์‚ฌ์šฉํ•จ
    • ์˜ค๋ธŒ์ ํŠธ ๋‚ด๋ถ€์˜ ํ‚ค๋ฅผ ๋ฐ˜๋ณต๋ฌธ์œผ๋กœ ์Šฌ๋ผ์ด์‹ฑํ•จ
    • for (key in ellie) { console.log(key); }
  2. for (value of iterable)
    • ์˜ค๋ธŒ์ ํŠธ ์ค‘ ์ˆœ์ฐจ์ ์œผ๋กœ ์ˆœํ™˜์ด ๊ฐ€๋Šฅํ•œ ๋ฐฐ์—ด(array)์˜ ๊ฒฝ์šฐ์— ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๋‚ด๋ถ€ ๊ฐ’์„ ๋ฐ˜๋ณตํ•˜๋Š” ๋ฉ”์„œ๋“œ
    • for (value of array) {} ํ˜•์‹์œผ๋กœ ์‚ฌ์šฉํ•จ
    • const array = [1,2,3,4]; for (value of array) { console.log(value); }

Clonning

  • ์˜ค๋ธŒ์ ํŠธ๋ฅผ ์„ ์–ธํ•˜๊ณ , ๋‹ค๋ฅธ ๋ณ€์ˆ˜์— ๋˜‘๊ฐ™์€ ์˜ค๋ธŒ์ ํŠธ๋ฅผ ๊ฐ€๋ฆฌํ‚จ๋‹ค๋ฉด ๋™์ผํ•œ ๋ ˆํผ๋Ÿฐ์Šค๊ฐ€ ํ• ๋‹น๋˜์–ด ์žˆ๋‹ค
  • ๋™์ผํ•œ ๋ ˆํผ๋Ÿฐ์Šค๋ฅผ ๋ณด๊ณ  ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์˜ค๋ธŒ์ ํŠธ ํ”„๋กœํผํ‹ฐ๋ฅผ ๋ณ€๊ฒฝํ•˜๋ฉด, ๋‹ค๋ฅธ ๋ณ€์ˆ˜์˜ ์˜ค๋ธŒ์ ํŠธ ํ”„๋กœํผํ‹ฐ๋„ ๋ณ€ํ•ด์žˆ๋‹ค
  • ์„œ๋กœ ๊ฐ™์€ ๋ ˆํผ๋Ÿฐ์Šค๋ฅผ ๋ณด๊ณ  ์žˆ์ง€๋งŒ, ์ƒˆ๋กญ๊ฒŒ ๋ณต์ œํ•ด์„œ ์‚ฌ์šฉํ•ด ๊ธฐ์กด ๋ ˆํผ๋Ÿฐ์Šค์˜ ๊ฐ’์„ ๋ณ€๊ฒฝํ•˜์ง€ ์•Š๋„๋ก ํ•˜๋ ค๋Š” ๋ฐฉ์‹์ด Clonning ์ด๋‹ค
  • ์ƒˆ๋กœ์šด ๋นˆ ์˜ค๋ธŒ์ ํŠธ๋ฅผ ์„ ์–ธํ•˜๊ณ , Object.assign ์„ ์‚ฌ์šฉํ•˜์—ฌ ์˜ค๋ธŒ์ ํŠธ๋ฅผ ํด๋กœ๋‹ํ•˜๋Š” ๊ฒƒ์ด ๊ฐ€๋Šฅํ•˜๋‹ค
const user4 = Object.assign({}, user);

const fruit1 = {color: 'blue'};
const fruit2 = {color: 'red', size:'big'};
// ๋‘ ๊ฐœ๋ฅผ ํ•ฉ์นจ
const fruit3 = {size:'small'};
const mixed = Object.assign({}, fruit1, fruit2, fruit3); // **red small** 
//๋’ค์— ์žˆ๋Š” ํ”„๋กœํผํ‹ฐ๊ฐ€ ๋ฎ์–ด ์”Œ์›€

'๊ฐœ๋ฐœ_TIL' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

๊ฐœ๋ฐœ_TIL | 2022-07-29 (71)  (0) 2022.07.31
๊ฐœ๋ฐœ_TIL | 2022-07-28 (70)  (0) 2022.07.31
๊ฐœ๋ฐœ_TIL | 2022-07-26 (68)  (0) 2022.07.26
๊ฐœ๋ฐœ_TIL | 2022-07-25 (67)  (0) 2022.07.26
๊ฐœ๋ฐœ_TIL | 2022-07-22 (66)  (0) 2022.07.22