๊ฐ์ฒด(object); ๊ฐ ๋ณ์์ ๊ฐ์ ์ข ๋ฅ, ๋ค๋ฅธ ๊ฐ์ ์ ๋ ฅํ๋ ์ฝ๋
// ์
๋ ฅํ ๋ฐ์ดํฐ ์์
let username : 'a';
let useremail : 'a@gmail.com';
let usercity : 'seoul';
let user2name : 'b';
let user2email : 'b@gmail.com';
let user2city : 'busan';
// ๋ฐฐ์ด๋ก ํํ
let user = ['a', 'a@gmail.com', 'seoul']
let user2 = ['b', 'b@gmail.com', 'busan']
// ๊ฐ ์ธ๋ฑ์ค๊ฐ ์ด๋ค ์ ๋ณด์ธ ์ง ์ ์ ์์.
// ๊ฐ์ฒด๋ก ํํ; ํค_name,email,city ๊ฐ_'a','a@gmail.com','seoul'
let user = {
name: 'a',
email: 'a@gmail.com',
city: 'seoul'
}
let user2 = {
name: 'b',
email: 'b@gmail.com',
city: 'busan'
}
let user = {
name: 'a',
email: 'a@gmail.com',
city: 'seoul'
}
// Dot natation
user.name; // 'a'
user.city; // 'seoul'
// Bracket natation
user['name']; // 'a'
user['city']; // 'seoul'
** user[name] ์ด๋ฐ์์ผ๋ก ๋ฐ์ดํ ์์ด ์ฌ์ฉํ๋ฉด ๊ฐ์ด ๋์ถ๋์ง ์๋๋ค. '',ย "", ``๋ชจ๋ ์ฌ์ฉ๊ฐ๋ฅ. ๋ง์ฝ name์ผ๋ก ์ด๋ค๋ฉด name์ด๋ผ๋ ๋ณ์์ ํค๋ฅผ ํ ๋นํ๋ฉด ์ฌ์ฉํ ์ ์์.
๊ฐ์ฒด์ ํค๋ฅผ ์ ๋ ฅ๋ฐ์ ํค์ ํด๋นํ๋ ๊ฐ์ ๋ฆฌํดํด์ผ ํฉ๋๋ค
CHECK POINT
์ ์ถ๋ ฅ ์์
const person = {
name: 'Steve',
age: 16,
};
let output = getProperty(person, 'name');
console.log(output); // --> 'Steve'
output = getProperty(person, 'age');
console.log(output); // --> 16
function getProperty(obj, property) {
let person = {
name: 'Steve',
age: 16,
};
return obj[property];
}
// ์
์ถ๋ ฅ
let output = getProperty(person, 'name');
console.log(output); // Uncaught ReferenceError: person is not defined
** ๋ณ์๋ก ์ ์ธํ person๊ฐ์ด ์ ์๋์ง ์์๋ค๋ ์ค๋ฅ ๋ฉ์์ง
=> ์ด๋ฒ์๋ ๋ณ์๋ฅผ ๋จผ์ ์ ์ธํ๊ณ ํจ์ ์งํ
let person = {
name: 'Steve',
age: 16,
};
function getProperty(obj, property) {
return obj[property];
}
// ์
์ถ๋ ฅ
let output = getProperty(person, 'name');
console.log(output); // Steve
** ์ถ๋ ฅ์ ์ ๋๋ก ๋์ง๋ง ์คํธ๋ง ๊ฐ์ผ๋ก ์ถ๋ ฅ ๋์ง ์์.
=> ๋ฆฌํด๊ฐ์ obj['property']๋ก ์์
let person = {
name: 'Steve',
age: 16,
};
function getProperty(obj, property) {
return obj['property'];
}
let output = getProperty(person, 'name');
console.log(output); // undefined
** 'property'๋ก ๊ฐ์ฒด๋ฅผ ๋ถ๋ฌ์ค๋ฉด person์ ํค๊ฐ(ex. name, age์ ๊ฐ์) ์ค์ property์ ๊ฐ์ ์ฐพ์ ๋ฐํํ๋ ๊ผด์ด ๋์ด๋ฒ๋ฆผ. ๋ณ์ person์ ํค๊ฐ์ผ๋ก property๊ฐ ์กด์ฌํ์ง ์์ undefined๋ก ์ต์ข
๊ฐ์ด ์ถ๋ ฅ.
๋ณ์๋ฅผ ์์ผ๋ก ๋นผ๊ณ ์คํํ ํจ์์ ๋ฆฌํด๊ฐ์ด ์ ๋ต. ๋ด๊ฐ ์์ํ ๊ฐ์ 'Steve'์๋๋ฐ, ๊ทธ๋ฅ Steve๋ก ์ถ๋ ฅ๋์ด ์ค๋ต์ผ๋ก ์๊ฐํจ. ๊ทธ๋์ ์์ ์์๋ ๋๊ฐ์ด '' ์์ด ์ถ๋ ฅ๋๋ ์ง ์ฝ์์ฐฝ์ ๋ค์ ์๋ํจ.
let name = 'steve'
let a = { content: 'ํ๋ฆฌ์ฝ์ค' };
a.content; // 'ํ๋ฆฌ์ฝ์ค'
a['content']; // 'ํ๋ฆฌ์ฝ์ค'
์ด๋๋ ์ ๋๋ก ''๊ฐ์ด ์ถ๋ ฅ๋จ. ๋ญ๊ฐ ๋ค๋ฅธ๊ฑธ๊น?
let person = {
name: 'Steve',
age: 16,
};
// Dot notation(1)
function getProperty(obj, property) {
return obj.name;
}
let output = getProperty(person, 'name');
console.log(output); // Steve
// ํญ์ ํค ์ค name ๊ฐ๋ง ์ถ๋ ฅ ๊ฐ๋ฅ. ๋ค๋ฅธ ํค๊ฐ์ ์ถ๋ ฅ ๋ถ๊ฐ๋ฅ(์ ์ )
// Dot notation(2)
function getProperty(obj, property) {
return obj.property;
}
let output = getProperty(person, 'name');
console.log(output); // undefined
// obj.property : person์ ํค ์ค property์ ๊ฐ์ ์ฐพ๋ ๊ฒ.
// Bracket notation
function getProperty(obj, property) {
return obj[property];
}
// ์
์ถ๋ ฅ
let output = getProperty(person, 'name');
console.log(output); // Steve
let output = getProperty(person, 'age');
console.log(output); // 16
// ๋ชจ๋ ํค์ ๊ฐ์ ๋ถ๋ฌ์ฌ ์ ์์.
let user = {
name: 'a',
email: 'a@gmail.com',
city: 'seoul'
}
// ํค 'hobby', ๊ฐ '๋
์' ์ถ๊ฐ
user['hobby'] = '๋
์'; // '๋
์'
user; // {
name: 'a', email: 'a@gmail.com', city: 'seoul', hobby: '๋
์'
}
// ํค 'isPublic', ๊ฐ 'true' ์ถ๊ฐ
user.isPublic = true;
true
user; // {
name: 'a', email: 'a@gmail.com', city: 'seoul', hobby: '๋
์', isPublic: true
}
// ํค email ์ญ์
delete user.email;
true
user; // {
name: 'a', city: 'seoul', hobby: '๋
์', isPublic: true
}
์์๋ ์ด๊ฑฐ ๊ฐ๋ฅํ ์์ฑ๋ค์ ํฌํจํ์ฌ ๊ฐ์ฒด์์ ๋ฌธ์์ด๋ก ํค๊ฐ ์ง์ ๋ ๋ชจ๋ ์ด๊ฑฐ ๊ฐ๋ฅํ ์์ฑ์ ๋ํด ๋ฐ๋ณต
const object = {
a: 1, b: 2, c: 3
};
// ํค(property)_a, b, c ๊ฐ_1, 2, 3
for (const property in object) { // ๋ณ์ object ์์ property(ํค)๋ฅผ ์ํํ๋ค.
console.log(`${property}: ${object[property]}`);
} // `${ํค}: ${๋ณ์[ํค]}`๋ฅผ ์ฝ์์ฐฝ์ ์ถ๋ ฅํ๋ค.
// expected output:
// "a: 1"
// "b: 2"
// "c: 3"
_mdn ์ฐธ์กฐ
tmi; ํ๋ฆฌ๊ฐ ๋ฌด์ง์ํ. ์ต๊ทผ์ ์ฑ ์ ์๋๋ฐ ๋ชจ์ ๋ฐ์คํฌ๋ก ์ด๊ป ํํ์ค.. ๊ทธ๋๋ ์์ดํจ๋๊ฐ ์์ด์ ๋คํ์ด๋ค๐ฅฒ ์ด์ ๋ ๋จธ๋ฆฌ๊ฐ ํฝํฝ ์ ๋์๊ฐ๋๋ฐ ์ค๋์ ๋ธ๋ ์ธํฌ๊ทธ์ฒ๋ผ ๊ณ์ ๋ฉํจ. ์ฝํ๋ฆฟ ํ๋ค๊ฐ ๊ณผ์๋จน์๋๋ ์ข ๋์..ใ ใ