- ๊ฐ์ฒด(์ฐธ์กฐ) ํ์ ์ ๊ฐ, ์ฆ ๋ณ๊ฒฝ ๊ฐ๋ฅํ ๊ฐ์ด๋ค.
- ์ฐธ์กฐ๊ฐ์ ๊ฐ์ง๊ณ ์์ผ๋ฉฐ, ์ฐธ์กฐ๊ฐ์ ์์ฑ๋ ๊ฐ์ฒด๊ฐ ์ ์ฅ๋ ๋ฉ๋ชจ๋ฆฌ ๊ณต๊ฐ์ ์ฃผ์๋ค!!
- ์์๊ฐ๊ณผ๋ ๋ค๋ฅด๊ฒ ์ฌ๋ฌ ๊ฐ์ ์๋ณ์๊ฐ ํ๋์ ๊ฐ์ฒด๋ฅผ ๊ณต์ ํ ์ ์๋ค๋ ๋ถ์์ฉ์ด ์๋ค.
- ์ฌ๋ฌ ๋ฐ์ดํฐ๋ฅผ Key:Value ํํ๋ก ์ ์ฅํฉ๋๋ค. { }
- method: ๊ฐ์ฒด ํ๋กํผํฐ๋ก ํ ๋น ๋ ํจ์
const superman = {
// this = {}
name: 'clark',
age: 32,
fly: function(){
console.log("๋ ์๊ฐ๋๋ค.");
}
// return this
}
๐พ #1
let a = 'age';
const user = {
name : 'Zooyaho',
[a] : 30 // age: 30
}
๐พ #2
const user = {
[1+4] : 5,
["์๋
" + "ํ์ธ์"]: "Hello"
}
console.log(user); // {5:5, ์๋
ํ์ธ์: "Hello"}
๐พ #์ ๊ทผ
superman.name; // 'clark'
superman['age']; // 32
๐พ #์ถ๊ฐ
superman.gender = 'male';
superman['hairColor'] = 'black';
๐พ #์ญ์
delete superman.hairColor;
const name = 'clark';
const age = 32;
/* const superman = {
name : name,
age : age,
gerder: 'male',
} */
const superman = {
name,
age,
gender: 'male',
}
const superman = {
name: clark,
age: 32,
}
console.log(superman.birthDay); // undefined
- ์ด๋ค๊ฐ์ด ๋์ด ์ค๋์ง ์ ์ ์์๋ in์ฐ์ฐ์ ์ฌ์ฉ
๐พ#1
console.log('birthDay' in superman); // false
๐พ#2
function makeObject(name, age) {
return {
name,
age,
hobby: 'football'
};
}
const Mike = makeObject("Mike", 30);
console.log(Mike);
// Object { age:30, hobby: "football", name: "Mike"}
console.log("age" in Mike); // true
console.log("birthDay" in Mike); // false
const superman = {
name: 'clark',
age: 32,
}
for( key in superman){
console.log(key); // "name"
console.log(superman[key]) // "clark"
}
/*
fly: function(){
console.log("๋ ์๊ฐ๋๋ค.");
}
*/
const superman = {
name: 'clark',
age: 32,
fly(){
console.log("๋ ์๊ฐ๋๋ค.");
}
}
- ํ์ดํ ํจ์๋ ์ผ๋ฐ ํจ์์๋ ๋ฌ๋ฆฌ ์์ ๋ง์ this๋ฅผ ๊ฐ์ง์ง ์์.
- ํ์ดํ ํจ์ ๋ด๋ถ์์ this๋ฅผ ์ฌ์ฉํ๋ฉด, ๊ทธ this๋ ์ธ๋ถ์์ ๊ฐ์ ๊ฐ์ ธ์ด.
๐๐ป ๊ฐ์ฒด์ ๋ฉ์๋๋ ์ผ๋ฐํจ์๋ก ์์ฑํ๋ ๊ฒ์ด ์ข์!!!
๐พ#1
const boy = {
name: 'Mike',
sayHello: function() {
console.log(`Hello, I'm ${this.name}`)
}
}
boy.sayHello(); // Hello, I'm Mike
๐๐ป ์ผ๋ฐํจ์ ๋ด์์์ this๋ ํด๋น ๊ฐ์ฒด๋ฅผ ๊ฐ๋ฆฌํด
๐พ#2
const girl = {
name: 'Jane',
sayHello: () => {
console.log(`Hello, I'm ${this.name}`)
}
}
girl.sayHello(); // Hello, I'm undefined
๐๐ป ํ์ดํํจ์ ๋ด์์์ this๋ ์ ์ญ๊ฐ์ฒด๋ฅผ ๊ฐ๋ฆฌํด