๊ฐ์ฒด๋ ์๋ฐ์คํฌ๋ฆฝํธ ๋ฐ์ดํฐ ํ์ ์ค์ ํ๋๋ก, ์ฌ๋ฌ ์์ฑ์ ํ๋์ ๋ณ์์ ์ ์ฅํ ์ ์๊ฒ ํด์ค๋ค. ๋ฐฐ์ด๊ณผ๋ ๋ค๋ฅด๊ฒ ์์๊ฐ ์ค์ํ์ง ์๊ธฐ ๋๋ฌธ์ ์ ๋ณด๋ฅผ ์ป๊ธฐ ์ํด Index number์ ๊ธฐ์ตํ์ง ์์๋ ๋๋ค.
์๋ฐ์คํฌ๋ฆฝํธ์ ๊ฐ์ฒด๋ key ๊ฐ๊ณผ key์ ํด๋นํ๋ ๋ฐ์ดํฐ์ธ value ๊ฐ์ด ์์ผ๋ก ์ด๋ฃจ์ด์ง property๋ก ๊ตฌ์ฑ๋๋ค. ๊ฐ์ฒด๋ {} ์์ key์ value๋ฅผ ์์ผ๋ก ๋ฌถ์ด ๋ง๋ค๊ณ , key๊ฐ์ด ์ฌ๋ฌ๊ฐ์ผ ๋๋ ,๋ก ๊ตฌ๋ถํ๋ค.
//๊ฐ์ฒด ๋ฆฌํฐ๋ด : ๋ณ์์ฒ๋ผ ๊ฐ์ฒด ์์ฑ
let myself = {
name : '์๋ค๋น',
bloodType: 'Aํ',
mbti: 'ENFJ',
};
console.log(myself);
//{ name : '์๋ค๋น', bloodType: 'Aํ', mbti: 'ENFJ'}
//์์ฑ์ ํจ์
let myself = new Object()
myself.name = '์๋ค๋น';
myself.bloodType = 'Aํ';
myself.mbti = 'ENFJ';
console.log(myself);
//{ name : '์๋ค๋น', bloodType: 'Aํ', mbti: 'ENFJ'}
๊ฐ์ฒด ์์ฑ ์ ๊ทผ ๋ฐฉ๋ฒ์ dot notation๊ณผ bracket notation ๋ ๊ฐ์ง๊ฐ ์๋ค.
dot notation
bracket notation
let obj = {
"hat": "ballcap",
"shirt": "jersey",
"shoes": "cleats"
};
//dotNotation
//๋ง์นจํ ์ฐ์ฐ์(.)๋ฅผ ์ฌ์ฉํ์ฌ ๊ฐ์ฒด์ property ๊ฐ์ ์ ๊ทผ
console.log(obj.hat) //"ballcap"
console.log(obj.shirt) //"jersey"
//Bracket Notation
console.log(obj['hat']) //"ballcap"
console.log(obj['shirt']) //"jersey"
//๋ณ์๋ฅผ ์ฌ์ฉํ ๊ฐ์ฒด ์์ฑ ์ ๊ทผ
let testObj = {
12: "Namath",
16: "Montana",
19: "Unitas"
};
let playerNumber = 16;
let player = testObj[playerNumber]
console.log(player); //"Montana"
//testObj.playerNumber์ undefined
let myDog = {
"name": "Coder",
"legs": 4,
"tails": 1,
"friends": ["freeCodeCamp Campers"]
};
myDog.friends = ['dabin', 'ball']
console.log(myDog['friends']); //["dabin", "ball"]
myDog['name'] = '๋ฌ๋ง์ด'
console.log(myDog.name);//"๋ฌ๋ง์ด"
let myDog = {
"name": "Happy Coder",
"legs": 4,
"tails": 1,
"friends": ["Wecode Bootcamp"]
};
myDog['bark'] = 'woof' // ๋๋ myDog.bark = 'woof'
console.log(myDog);
/*
{
bark: "woof",
friends: ["Wecode Bootcamp"],
legs: 4,
name: "Happy Coder",
tails: 1
}
*/
let ourDog = {
"name": "Camper",
"legs": 4,
"tails": 1,
"friends": ["everything!"],
"bark": "bow-wow"
};
delete ourDog.bark; //๋๋ delete ourDog['bark']
console.log(ourDog);
/*
{
friends: ["everything!"],
legs: 4,
name: "Camper",
tails: 1
}
*/
let myStorage = {
"car": {
"inside": {
"glove box": "maps",
"passenger seat": "crumbs"
},
"outside": {
"trunk": "jack"
}
}
};
gloveBoxContents = myStorage['car']['inside']['glove box'];
console.log(gloveBoxContents); //"maps"
์ค์ฒฉ ๊ฐ์ฒด์ ๋ฐฐ์ด์ด ํฌํจ๋์ด ์์ ๋ ์ ๊ทผ๋ฒ์ index number์ ํ์ฉํ๋ ๊ฒ์ด๋ค.
let myPlants = [
{
type: "flowers",
list: [
"rose",
"tulip",
"dandelion"
]
},
{
type: "trees",
list: [
"fir",
"pine",
"birch"
]
}
];
let foundValue = myPlants[1]['list'][1] //๋๋ myPlants[1].list[1]
console.log(foundValue); //"pine"
console.log(typeof [1, 2, 3]) //"object"
์ด๋ค ์์ฑ์ ํจ์์ด๋ prototype(์ํ)์ ๋ฐ๋์ ๊ฐ์ฒด๊ธฐ ๋๋ฌธ์ Object.prototype์ด ์ธ์ ๋ ํ๋กํ ํ์
์ฒด์ธ ์ต์๋จ์ ์กด์ฌํ๋ค. ๋ฐฐ์ด์ ํ๋กํ ํ์
์ Array.prototype์ด๊ณ , ์๋ต ๊ฐ๋ฅํ __proto__
๋ฅผ ๋ฐ๋ผ Object.prototype์ ์ฐธ์กฐํ ์ ์๋ค. ๋ฐ๋ผ์ ๋ฐฐ์ด์ ํ์
๋ ๊ฐ์ฒด๋ค.
https://velog.io/@dabin0219/์ฝ์ด-์๋ฐ์คํฌ๋ฆฝํธ-06ํ๋กํ ํ์
-07ํด๋์ค