Skip to content

Latest commit

ย 

History

History
172 lines (132 loc) ยท 5.22 KB

Object.md

File metadata and controls

172 lines (132 loc) ยท 5.22 KB

Object

1. ๊ฐ์ฒด๋ž€?

  • JavaScript์—์„œ์˜ ๊ฐ์ฒด๋Š” ๊ฒŒ์ž„ ์บ๋ฆญํ„ฐ์™€ ๋น„์Šทํ•˜๋‹ค๊ณ  ํ•  ์ˆ˜ ์žˆ๋‹ค. ์‚ฌ์šฉ์ž๋“ค์˜ ์บ๋ฆญํ„ฐ๋Š” ๋™์ผํ•˜๊ฒŒ ์ง์—…๊ณผ ๋Šฅ๋ ฅ์„ ๊ฐ€์ง€๊ณ  ์žˆ์ง€๋งŒ, ๊ฐ๊ฐ ์„ธ๋ถ€์ ์ธ ๋‚ด์šฉ์€ ๋‹ค๋ฅด๋‹ค.

  • ๋ˆ„๊ตฐ๊ฐ€๋Š” Ella๋ผ๋Š” ID์™€ ๋งˆ๋ฒ•์‚ฌ๋ผ๋Š” ์ง์—…์„ ๊ฐ€์ง€๊ณ  ์žˆ์ง€๋งŒ, ๋‹ค๋ฅธ ๋ˆ„๊ตฐ๊ฐ€๋Š” Chloe๋ผ๋Š” ID์™€ ์ „์‚ฌ๋ผ๋Š” ์ง์—…์„ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค.

  • ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ์‚ฌ์šฉ์ž๊ฐ€ ์›น์‚ฌ์ดํŠธ์— ๊ฐ€์ž…ํ•  ๋•Œ ์ž…๋ ฅํ•  ํ•ญ๋ชฉ์€ ๋ชจ๋‘ ๊ฐ™์ง€๋งŒ, ์ž…๋ ฅํ•˜๋Š” ์ •๋ณด๋Š” ์‚ฌ์šฉ์ž๋งˆ๋‹ค ๋‹ค๋ฅด๋‹ค. ์ด๋ ‡๊ฒŒ ๊ฐ๊ธฐ ๋‹ค๋ฅธ ๊ฐ’์„ ๊ฐ€์งˆ ์ˆ˜ ์žˆ์ง€๋งŒ, ์ž…๋ ฅํ•ด์•ผ ํ•˜๋Š” ๋ฐ์ดํ„ฐ์˜ ์ข…๋ฅ˜๊ฐ€ ๋™์ผํ•œ ๊ฒฝ์šฐ ๊ฐ์ฒด๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์†์‰ฝ๊ฒŒ ๋ฐ์ดํ„ฐ๋ฅผ ๊ด€๋ฆฌํ•  ์ˆ˜ ์žˆ๋‹ค.

  • ์ด๋ ‡๋“ฏ, ๊ณตํ†ต์ ์ธ ์†์„ฑ์„ ๊ฐ€์ง€๋Š” ๊ฒฝ์šฐ ๊ฐ์ฒด๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค. ์›น์‚ฌ์ดํŠธ์— ๊ฐ€์ž…ํ•œ ํšŒ์› ์ฃผ์†Œ๋ก์„ ๋งŒ๋“ ๋‹ค๊ณ  ๊ฐ€์ •ํ•ด ๋ณด์ž.

    // ์œ„์™€ ๊ฐ™์ด ๋งค๋ฒˆ ์—ฌ๋Ÿฌ ๊ฐœ์˜ ๋ณ€์ˆ˜๋ฅผ ์„ ์–ธํ•ด ์ฃผ์–ด์•ผ ํ• ๊นŒ?
    let userFirstName = 'Ella';
    let userLastName = 'Choi';
    let userEmail = 'ella@gmail.com';
    let userCity = 'Seoul';
    
    let userFirstName = 'Chloe';
    let userLastName = 'Kim';
    let userEmail = 'chloe@gmail.com';
    let userCity = 'Seoul';
    let user = { // ์ค‘๊ด„ํ˜ธ๋กœ ๊ฐ์ฒด ๋งŒ๋“ฌ
      firstName: 'Ella', // ํ•œ ์Œ์˜ ์ด๋ฆ„๊ณผ ๊ฐ’์€ ',' ๋กœ ๊ตฌ๋ถ„, ์ด๋ฆ„๊ณผ ๊ฐ’์€ ':' ์œผ๋กœ ๋ถ„๋ฆฌ
      lastName: 'Choi', // ์—ฌ๊ธฐ์„œ lastName๋Š” ํ‚ค(key), 'Choi'๋Š” ๊ฐ’(value)
      email:'ella@gmail.com',
      city: 'Seoul'
      }

2. ๊ฐ์ฒด์˜ ๊ฐ’์„ ์กฐํšŒํ•˜๋Š” ๋ฐฉ๋ฒ• ๋‘ ๊ฐ€์ง€

Dot notation: ๊ฐ์ฒด + .(์ ) + ํ‚ค ๊ฐ’

let user = { 
 firstName: 'Ella', 
 lastName: 'Choi', 
 email:'ella@gmail.com',
 city: 'Seoul'
};

// ํ˜•์‹: ๊ฐ์ฒด + .(์ ) + ํ‚ค ๊ฐ’
user.firstName; // 'Ella'
user.city; // 'Seoul'

Bracket notation: ํ‚ค ๊ฐ’์ด ๋Œ€๊ด„ํ˜ธ ์•ˆ์— '๋ฌธ์ž์—ด'

let user = {
 firstName: 'Ella', 
 lastName: 'Choi', 
 email:'ella@gmail.com',
 city: 'Seoul'
};

// ํ˜•์‹: ํ‚ค ๊ฐ’์ด ๋Œ€๊ด„ํ˜ธ ์•ˆ์— '๋ฌธ์ž์—ด'๋กœ ๋“ค์–ด๊ฐ
user['firstName']; // 'Ella'
user['city']; // 'Seoul'

3. ๊ฐ์ฒด๋ฅผ ๋‹ค๋ฃจ๋Š” ๋‹ค์–‘ํ•œ ๋ฐฉ๋ฒ•

Bracket notation: ํ‚ค ๊ฐ’์ด ๋™์ ์œผ๋กœ ๋ณ€ํ•  ๋•Œ ๋ฐ˜๋“œ์‹œ ์‚ฌ์šฉ

let person = {
name: 'Ella',
age: 16 
}; // ํ‚ค ๊ฐ’์ด ๊ณ„์† ๋ณ€ํ•˜๋Š” ์ƒํ™ฉ

// name ๊ฐ’ ์กฐํšŒ
let output = getProperty(person, 'name');
console.log(output); // -> 'Ella'

// name ๊ฐ’ ์กฐํšŒ ๋ฐฉ๋ฒ•
function getProperty(obj, propertyName) {
  return obj[propertyName]; // obj['name']   
}

// ์ฃผ์˜1: return person.name (X) Ella๋งŒ ๋‚˜์˜ค๊ธฐ์— -> obj[propertyName]๋ฅผ ๋„ฃ์–ด์•ผ ํ•จ
// ์ฃผ์˜2: return.propertyName (X) -> propertyName ๋ผ๋Š” ํ‚ค ์ด๋ฆ„์ด ๋ณ„๋„๋กœ ์žˆ์–ด์•ผ ๋„ฃ์„ ์ˆ˜ ์žˆ์Œ


// age ๊ฐ’ ์กฐํšŒ
let output = getProperty(person, 'age');
console.log(output2); // -> 16

// age ๊ฐ’ ์กฐํšŒ ๋ฐฉ๋ฒ•
function getProperty(obj, propertyName) {
  return obj[propertyName]; // obj['age']   
}

dot/bracket notation์œผ๋กœ ๊ฐ’์„ ์ถ”๊ฐ€ํ•  ์ˆ˜๋„ ์žˆ์Œ

let tweet = {
writer: 'Ella Choi',
createdAt: '2022-02-28 12:03:33',
content: '์ฝ”๋”ฉ์€ ์žฌ๋ฐŒ์–ด'
};

// ๋‹ค์–‘ํ•œ ๊ฐ’์„ ๋„ฃ์„ ์ˆ˜ ์žˆ์Œ
tweet['category'] = '์ด์•ผ๊ธฐ'; // Bracket notation์œผ๋กœ ์ด์•ผ๊ธฐ ๋ผ๋Š” ๋ฌธ์ž์—ด์„ ์ถ”๊ฐ€
tweet.isPublic = true; // Dot notation์œผ๋กœ Boolean๊ฐ’์„ ์ถ”๊ฐ€
tweet.tags = ['#์ฝ”๋”ฉ', '#๊ฐœ๋ฐœ์ž'] // Dot notation์œผ๋กœ ๋ฐฐ์—ด์„ ์ถ”๊ฐ€

delete ํ‚ค์›Œ๋“œ๋กœ ์†์„ฑ ์‚ญ์ œ๋„ ๊ฐ€๋Šฅ

let tweet = {
  writer: 'Ella Choi',
  createdAt: '2022-02-28 12:03:33',
  content: '์ฝ”๋”ฉ์€ ์žฌ๋ฐŒ์–ด'
};

delete tweet.createdAt; // createAt ํ‚ค-๊ฐ’ ์Œ์„ ์ง€์›€
tweet.createdAt // ์ง€์›Œ์กŒ์œผ๋‹ˆ ์ฝ˜์†”์— ์ฐ์œผ๋ฉด undefined๋กœ ๋‚˜์˜ด

// tweet์€ ๋‹ค์Œ๊ณผ ๊ฐ™๊ฒŒ ๋จ
// {writer: 'Ella Choi', content: '์ฝ”๋”ฉ์€ ์žฌ๋ฐŒ์–ด'}

in ์—ฐ์‚ฐ์ž๋กœ ํ•ด๋‹นํ•˜๋Š” ํ‚ค๊ฐ€ ์žˆ๋Š”์ง€ ํ™•์ธ ๊ฐ€๋Šฅ

let tweet = {
writer: 'Stevelee',
createdAt: '2022-02-28 12:03:33',
content: '์ฝ”๋”ฉ์€ ์žฌ๋ฐŒ์–ด'
};

'content' in tweet; // true
'updatedAt' in tweet; // false

4. ๋ฌธ์ž์—ด ์ž‘์„ฑ ์‹œ ํ”ํžˆ ํ•˜๋Š” ์‹ค์ˆ˜

Bracket notation ์‚ฌ์šฉ ์‹œ ๋Œ€๊ด„ํ˜ธ๋ฅผ ๋นผ๋จน๋Š”๋‹ค๊ฑฐ๋‚˜, ๋ณ€์ˆ˜ ์„ ์–ธ์„ ํ•˜์ง€ ์•Š์•˜๋‹ค๊ฑฐ๋‚˜ ๋“ฑ๋“ฑ ์‚ฌ์†Œํ•œ ์‹ค์ˆ˜๋ฅผ ๋ง‰๊ธฐ ์œ„ํ•ด ์•„๋ž˜ ๋‚ด์šฉ์„ ํ•˜๋‚˜์”ฉ ์ดํ•ดํ•˜๋ฉฐ ๋ˆˆ์— ์ตํ˜€๋ณด์ž!

let user = {
firstName: 'Ella',
lastName: 'Choi',
email:'ella@gmail.com',
city: 'Seoul'
};
user['firstName']; // 'Ella' : Bracket notation์œผ๋กœ ๊ฐ์ฒด์˜ ๊ฐ’ ์กฐํšŒ
user[firstName]; // ReferenceError: firstName is not defined : firstName์ด ๋ณ€์ˆ˜์ทจ๊ธ‰ ๋˜๊ณ  ์žˆ๋Š” ๊ฒƒ์ž„

let keyname = 'firstName'; // keyname์— 'firstName'๋ผ๋Š” ๋ฌธ์ž์—ด์„ ๋‹ด์•„๋ด„
user[keyname] // 'Ella' : 'firstName' ๋ฌธ์ž์—ด ๊ฐ’์ด keyname๋กœ ๋“ค์–ด๊ฐ”๊ธฐ ๋•Œ๋ฌธ์—
user[keyname] === user['firstName'] // true : ์‚ฌ์‹ค์ƒ Bracket notation์œผ๋กœ ๊ฐ์ฒด ๊ฐ’ ์กฐํšŒํ•œ ๊ฒฐ๊ณผ์™€ ๊ฐ™์Œ

let firstName = 'Ella'
user[firstName] === user['firstName'] // false : Bracket notation ์‚ฌ์šฉ ์‹œ ๋Œ€๊ด„ํ˜ธ ์•ˆ์— ๋ฌธ์ž์—ด์ด ๋“ค์–ด๊ฐ€์•ผ ํ•จ
user[keyname] === user['firstName'] // true

user['firstName'] === user.firstName // true :Dot notation === Bracket notation
user[firstName] === user.firstName // false : Bracket notation ์‚ฌ์šฉ ์‹œ ๋Œ€๊ด„ํ˜ธ ์•ˆ์— ๋ฌธ์ž์—ด์ด ๋“ค์–ด๊ฐ€์•ผ ํ•จ
user[firstName] === user['firstName'] // false