๐ง Object (2)
๊ฐ์ฒด๋ ๋ค๋ฅธ ๋ฐ์ดํฐ ํ์ (ํ ์คํธ, ์ซ์, ๋ฐฐ์ด ๋ฑ..)์ฒ๋ผ ๋ณ์์ ์ ์ฅํ ์ ์์ต๋๋ค.{}
๋ง ์ฌ์ฉํ๋ฉด ๋๋๋ฐ์, ์ด๋ ๊ฒ {} ์ผ๋ก ์๊ธด ๋ชจ์์ ๊ฐ์ฒด๋ฅผobject literal(๊ฐ์ฒด ๋ฆฌํฐ๋ด)
์ด๋ผ๊ณ ๋ถ๋ฆ ๋๋ค.
hello๋ผ๋ ๋ณ์์ ๋น ๊ฐ์ฒด๋ฅผ ํ ๋นํ์์ต๋๋ค.let hello = {}; // {} ๊ฐ์ฒด ๋ฆฌํฐ๋ด
์์๊ฐ ์๋ ๋ฐ์ดํฐ๋ ๋ฐฐ์ด์ ๋๋ค.
let arr = ['๊ณ ์์ด', '๊ท์ฌ์', '๋ฉ๋ฉ์ด', '์ข์'];
arr[0]์ '๊ณ ์์ด' ์ด๊ณ , arr[1]์ '๊ท์ฌ์' ์ ๋๋ค.
๊ฐ์ฒด๋ ํค(key)-๊ฐ(value) ์์ผ๋ก๋ ๋ฐ์ดํฐ์ ๋ชจ์์ผ๋ก์จ ์์๊ฐ ๋ค๋ฐ๊ปด๋ ์๋ฌด ์๊ด์ด ์์ต๋๋ค. ๋ฐฐ์ด์ฒ๋ผ ์์๋๋ก index๋ก ์ ๊ทผํ๋๊ฒ ์๋๋ผ ํค๋ก ์ ๊ทผํ๊ธฐ ๋๋ฌธ์ ๋๋ค.
key
๋property name
์ด๋ผ๊ณ ๋ ํ๋ฉฐ,value
๋property value
๋ผ๊ณ ๋ ๋งํฉ๋๋ค.
ํค๋ ๋ง์น ํน์ ๊ฐ์ ๊ฐ๊ณ ์๋ ๋ณ์๊ฐ์ ์ญํ ์ ํฉ๋๋ค. ํด๋น ํค๋ก ํ๋กํผํฐ์ ์ด๋ค ๊ฐ์ด ์ ์ฅ๋์๋์ง ์ ์ ์์ต๋๋ค. ํค์ ๊ฐ์๋ ํ ์คํธ, ์ซ์ ๋ฟ๋ง ์๋๋ผ ํจ์, ๊ฐ์ฒด๋ ๋ฃ์ ์ ์์ต๋๋ค.let difficult = { 'my name': 'boong', color: 'silver', ํค: 'ํ๊ธ์ธ ํค๋ ๋ฐ์ดํ๊ฐ ์์ด๋ ๋๋๊ตฐ!!', '!ํค': '๋๋ํ ์๋ ํค๋ ๋ฐ์ดํ๊ฐ ํ์ํ๊ตฐ', $special: '$๋ ์์ด๋ ๋๋๊ตฐ' };
๊ฐ์ฒด์ ํค์๋ ์คํ์ด์ค, ํ๊ธ, ํน์๋ฌธ์ ๋ฑ์ด ๋ค์ด๊ฐ ์ ์์ต๋๋ค. ๋ณ์(variable) ์ ์ธํ ๋๋ ํ ์ ์์๋ ํํ์ ๋๋ค. color์ ๊ฐ์ด ํค์ ํน์๋ฌธ์๊ฐ ์์ผ๋ฉด ๋ฐ์ดํ๋ฅผ ์๋ตํ๊ณ ์ธ ์ ์์ต๋๋ค.
๐คฒ ํ๋กํผํฐ ์ ๊ทผ
์ ๊ทผ์ dot(.)์ผ๋ก ์ ๊ทผํ๋ ๋ฐฉ๋ฒ๊ณผ, [] ๋๊ดํธ๋ก ์ ๊ทผํ๋ ๋ฒ์ด ์์์ต๋๋ค.let difficult = { 33: '์ซ์ ํ์๋ ๋๋ค', 'my name': '์คํ์ด์ค ํฌํจ ๊ฐ๋ฅ', color: 'silver', ํค: 'ํ๊ธ์ธ ํค๋ ๋ฐ์ดํ๊ฐ ์์ด๋ ๋๋๊ตฐ!!', '!ํค': '๋๋ํ ์๋ ํค๋ ๋ฐ์ดํ๊ฐ ํ์ํ๊ตฐ', $special: '$๋ ์์ด๋ ๋๋๊ตฐ' };
console.log(difficult.color);
์ฌ์ค ํค๋ง ์๊ณ ์์ผ๋ฉด dot(.)์ผ๋ก ์ ๊ทผํ๋ ๊ฒ์ด ์ ์ผ ํธํฉ๋๋ค. dot(.)์ ํค๋ก ๋ฐ๋ก ์ ๊ทผํ ๋ ์ฌ์ฉํ๋ ๊ฒ์ ๋๋ค. dot์ผ๋ก ์ ๊ทผํ ๋๋ ๋ฐ์ดํ ์์ด ํค๋ฅผ ๋ฐ๋ก ์จ์ค์ผ ํฉ๋๋ค. ๊ทธ๋ฐ๋ฐ [] ๋๊ดํธ๋ก ์ ๊ทผํ ์๋ ์์์ฃ !
console.log(difficult['color']);
๋๊ดํธ๋ก ์ ๊ทผํ๋ ค๋ฉด ํค ์ด๋ฆ์ ๋ฐ์ดํ๋ก ๊ฐ์ธ์ ์์ฑํด์ค์ผ ํฉ๋๋ค.
console.log(difficult.my name);
์์ ์์์ฒ๋ผ ์คํ์ด์ค๊ฐ ํฌํจ๋ ํค๋ ๋๊ดํธ๋ก ์ ๊ทผํ๋ฉด ๋ฉ๋๋ค.
console.log(difficult['my name']); // ์ด๋ ๊ฒ.
console.log(difficult.33);
์์ ์์์ฒ๋ผ ์ซ์๋ก ๋ ํค ๋ํ ๋๊ดํธ๋ก ์ ๊ทผํด์ผ ํฉ๋๋ค.
console.log(difficult['33']); // ์ด๋ ๊ฒ.
์ธ์ธ ํ์๋ ์์ด~
์ค๋ฅ ๋จ๋ฉด ๊ทธ ๋ ์ค๋ฅ ๋ฉ์์ง๋ฅผ ํ์ธํ๊ณ ์์ ํ๋ฉด ๋ฉ๋๋ค! ์ด๋ ๊ฒ ๋ค์ํ ํํ๋ก ํค ์ด๋ฆ์ ์ง์ ์ ์๋ค๋ ๊ฒ๋ง ์์์ฃผ์ธ์.
๐คฒ ๋ณ์๋ก ํ๋กํผํฐ ์ ๊ทผํ๊ธฐ
let name = 'ํค'; //์ฌ๊ธฐ์ name ์ key ๊ฐ ์๋๋ผ, ๋ณ์๋ก ์ ์ธ์ ํด์คฌ์๋ฟ! console.log(difficult[name]);
๋ณ์์ ํค ์ด๋ฆ์ด ์ ์ฅ๋์ด์์ผ๋ฉด, ๋ณ์๋ก๋ ํ๋กํผํฐ์ ์ ๊ทผ ๊ฐ๋ฅํฉ๋๋ค.
์ด๋ dot(.) ๋ค์์ ๋ณ์๋ช ์ ๋ฃ์ด์ ํ๋กํผํฐ์ ์ ๊ทผ ๊ฐ๋ฅํ ๊น์?
'name'์ด๋ผ๋ ํค๊ฐ ์์์ผ๋ฉด ๊ทธ์ ํด๋นํ๋ ํ๋กํผํฐ์ ์ ๊ทผํ ์ ์์๊ฒ ์ง๋ง ์กด์ฌํ์ง ์๊ธฐ ๋๋ฌธ์ undefined๊ฐ ๋์ต๋๋ค. ๋ณ์๋ก ์ ๊ทผํ ๋๋ ํญ์ ๋๊ดํธ๋ก ์ ๊ทผํด์ผ ํฉ๋๋ค.
๐คฒ ํ๋กํผํฐ ํ ๋น
difficult[name] = '๊ฐ ๋ฐ๊พผ๋ค'; console.log(difficult[name]); difficult.color = '์๊น'; console.log(difficult.color); console.log('์์ฑ์ : ' + difficult.new); difficult.new = '์๋ก ์ถ๊ฐ๋ ํ๋กํผํฐ'; console.log('์์ฑํ: ' + difficult.new);
๊ฐ์ฒด์ ์ด๋ฏธ ํค๊ฐ ์กด์ฌํ๋๋ฐ, ๋ค์ ํ ๋ฒ ํ ๋นํ๋ฉด ๊ฐ์ด ๊ต์ฒด(์์ )๋ฉ๋๋ค.
์ด์ ์ ์๋ ํค๋ก ์ ๊ทผํ๋ฉด, ์๋ก์ด ํ๋กํผํฐ๊ฐ ์ถ๊ฐ ๋ฉ๋๋ค.
์์ง ์๋ ํค์ ์ ๊ทผํ๋ฉด ํ๋กํผํฐ๋ฅผ ์ถ๊ฐํ ์ค๋น๋ ๋์ด์์ง๋ง, ๊ฐ์ด ์์ต๋๋ค.console.log(difficult.newProperty);
์์ง difficult ๋ผ๋ ๊ฐ์ฒด์ newProperty ํ๋กํผํฐ๊ฐ ์๊ธฐ ๋๋ฌธ์ undefined์ ๋๋ค.
difficult.realNewProperty = '์ถ๊ฐ ๋๋ค';
์์ ์์๋ ์์๋ ํค์ ๊ฐ์ ๋ฐ๋ก ์ ๋ ฅํด์ ํ๋กํผํฐ๋ฅผ ์ถ๊ฐํ๋ ๋ฐฉ๋ฒ์ ๋๋ค.
const a = 1; a = 2;
const๋ก ์ ์ธ๋ ๋ณ์๋ ๊ฐ์ ์ ๋ ์์ ํ ์ ์์์ต๋๋ค. a = 2;๋ฅผ ์๋ํ๋ฉด ์๋ฐ์คํฌ๋ฆฝํธ ์ค๋ฅ๊ฐ ๋ฉ๋๋ค.
๊ทธ๋ฐ๋ฐ const๋ก ์ ์ธ๋ ๋ณ์์ ๊ฐ์ฒด๋ฅผ ๋ค์ ํ ๋นํ๋ฉด ์ค๋ฅ๊ฐ ์๊ธฐ์ง๋ง, ๊ทธ ๊ฐ์ฒด์ ํ๋กํผํฐ๋ฅผ ์ถ๊ฐํ๊ฑฐ๋ ์์ ํ๋ ๊ฒ์ ๊ฐ๋ฅํฉ๋๋ค.const mutableObj = { name: '๊ฐ์ฒด' }; mutableObj = { name: '์์ ' //'๊ฒ์ฒด' ๋ผ๋ ํ๋กํผํฐ๊ฐ '์์ ' ์ด๋ผ๋ ํ๋กํผํฐ๋ก ๋ณ๊ฒฝ๋จ. }
๋ณ์ mutableObj ์์ฒด์ ๊ฐ์ฒด๋ฅผ ์ฌ ํ ๋นํ๋ ๊ฒ์ ๋ถ๊ฐ๋ฅ ํฉ๋๋ค. ์คํํด๋ณด๋ฉด ์๋ฐ์คํฌ๋ฆฝํธ ์ค๋ฅ๊ฐ ๋ฉ๋๋ค.
mutableObj.name = '์์ '; //์ค๋ฅ๋ฐ์. mutableObj.type = 'Object ํ์ '; //์ค๋ฅ๋ฐ์.
๐คฒ Method(๋ฉ์๋)
๊ฐ์ฒด์ ์ ์ฅ๋ ๊ฐ์ด ํจ์์ผ ๋, ๋ฉ์๋๋ผ๊ณ ๋ถ๋ฆ ๋๋ค.console.log(); //์ฝ์๋ ๊ฐ์ฒด์ด๋ค!!
์๋ฐ์คํฌ๋ฆฝํธ ์ด๋์๋ ์ ๊ทผ์ด ๊ฐ๋ฅํ์ผ๋
global ๊ฐ์ฒด
์ ๋๋ค. console๋ค์์ dot('.')์ผ๋ก ํ๋กํผํฐ๋ฅผ ์ ๊ทผํ๊ณ ,log
๋ผ๋ ํค์ ๊ฐ์ ํจ์์ธ ๊ฒ ๊ฐ์ต๋๋ค. log๋ console์ด๋ผ๋ ๊ฐ์ฒด์ ๋ฉ์๋์ ๋๋ค.let methodObj = { do: function() { console.log('๋ฉ์๋ ์ ์๋ ์ด๋ ๊ฒ'); } }
methodObj.do(); //ํธ์ถํ๊ธฐ.
๐คฒ ์ค์ฒฉ๋ ๊ฐ์ฒด (Nested Object)
์ค๋ฌด์์ ์ฌ์ฉ๋๋ ๊ฐ์ฒด๋ ๊ฑฐ์ ์ค์ฒฉ๋์ด ์์ต๋๋ค. ํ๋กํผํฐ ๊ฐ์ด ๊ฐ์ฒด์ผ ์๋ ์๊ณ , ํ๋กํผํฐ ๊ฐ์ธ ๋ฐฐ์ด์ ์์๊ฐ ๊ฐ์ฒด์ผ ์๋ ์์ต๋๋ค.let nestedObj = { //๊ฐ์ฒด ์์ type: { year: '2019', 'comment-type': [{ //ํ๋กํผํฐ ๊ฐ์ธ ๋ฐฐ์ด์ ์์๊ฐ ๊ฐ์ฒด name: 'simple' }] } }
์์์ 'simple'์ ์ถ๋ ฅํ๊ณ ์ถ๋ค๋ฉด?
console.log(nestedObj.type['comment-type'][0].name); /* ```nestedObj```๋ณ์์ ์ ๊ทผ -> ๊ฐ์ฒด ```type```์ ์ ๊ทผ -> ๊ฐ์ฒด```['comment-type']```์ ์ ๊ทผ (์ค๊ดํธ์ธ ์ด์ : ๊ฐ์ฒด ์ด๋ฆ์ '' ๋ฐ์ดํ๊ฐ ์ฌ์ฉ๋๊ธฐ ๋๋ฌธ์) ๊ทธ๋ฆฌ๊ณ ๊ฐ์ฒด ```'comment-type'```์ ๊ฐ์ด ๋ฐฐ์ด๋ก ์ด๋ฃจ์ด์ก๊ณ ๊ทธ ๋ฐฐ์ด์ ์์ ๋ํ ๊ฐ์ฒด์ด๊ธฐ ๋๋ฌธ์ [0] ๋ฒ์งธ์ ๊ฐ์ฒด์ธ ```name```์ ์ ๊ทผํ์ฌ simple์ ์ถ๋ ฅํ ์ ์๋ค!!*/
์ปดํจํฐ๋ ์ผ์ชฝ์์ ์ค๋ฅธ์ชฝ์ผ๋ก ํด์ํ ๋ค๋ ๊ฒ์ ๋ช ์ฌํ๊ณ ์ดํดํด์ฃผ์ธ์.
ํญ์ ์ผ์ชฝ์ ์ง์ ์ฝ๋๋ฅผ ์จ์ผ ํ๋ค๋ ๊ฒ์ ์์ง ๋ง์์ฃผ์ธ์!!
- nestedObj.type๋ year, comment-type์ด ์๋ ๊ฐ์ฒด์ ๋๋ค
- nestedObj.type['comment-type']๋ ์์ 1๊ฐ์ธ ๋ฐฐ์ด์ ๋๋ค.
- nestedObj.type['comment-type'][0] ์ฒซ ๋ฒ์งธ ์์์ธ ๊ฐ์ฒด๋ฅผ ๊ฐ์ ธ์ต๋๋ค.
- nestedObj.type['comment-type'][0].name ๋๋์ด 'simple'์ ์ ๊ทผ!
๐คฒ ๊ฐ์ฒด๋ reference๋ก ์ ์ฅ๋๋ค.
๊ฐ์ฒด๋ฅผ ๋ณ์์ ์ ์ฅํ๋ฉด ๊ฐ์ฒด ๋ฆฌํฐ๋ด ์์ฒด๊ฐ ์ ์ฅ๋๋ ๊ฒ์ด ์๋๋ผ reference๊ฐ ์ ์ฅ๋ฉ๋๋ค. ํ ์คํธ๋ ๋ณ์์ ์ ์ฅํ๋ฉด ํ ์คํธ ์์ฒด๊ฐ ์ ์ฅ๋ฉ๋๋ค. ๊ทธ๋์ ๊ฐ์ ํ ์คํธ๋ฉด ์๋ก ๊ฐ์ด ๊ฐ์ผ๋ฏ๋ก true์ ๋๋ค.const a = '์๋ '; const b = '์๋ '; console.log(a === b); //true
๊ทธ๋ฐ๋ฐ ์๋์ ๊ฐ์ฒด๋ ์๊ธด ๋ชจ์์ด ์์ ๋๊ฐ์๋ฐ false๋ผ๊ณ ์ถ๋ ฅ๋ฉ๋๋ค.
const hiObj = { name: '์๋ ' }; const helloObj = { name: '์๋ ' }; console.log('๊ฐ์ฒด๋น๊ต =>', hiObj === helloObj); //false
๊ทธ ์ด์ ๋ ๊ฐ์ฒด๋ ๋ณ์์ ์ ์ฅํ ๋, ๊ฐ์ฒด ์์ฒด๋ฅผ ๊ทธ๋๋ก ์ ์ฅํ๋ ๊ฒ์ด ์๋๊ธฐ ๋๋ฌธ์ ๋๋ค. ๊ฐ์ฒด๊ฐ ๋ด๊ธด ์ด๋ ๋ฉ๋ชจ๋ฆฌ์ reference ๋ฅผ ์ ์ฅํ๊ธฐ ๋๋ฌธ์ ๋๋ค.
hiObj๊ฐ ๊ฐ๊ณ ์๋ ์ง์ง ๊ฐ์ ๋ฉ๋ชจ๋ฆฌ ์ฃผ์์ธ reference์ ๋๋ค. ํ์ง๋ง hiObj๋ฅผ ๋ถ๋ฌ์ฌ ๋ ๋ฉ๋ชจ๋ฆฌ ์ฃผ์๋ฅผ ๋ฐํํ๋ ๊ฒ์ด ์๋๋ผ, ํด๋น ๋ฉ๋ชจ๋ฆฌ์ ์ ์ฅ๋ ๋ฐ์ดํฐ๋ฅผ ๋ฐํํด ์ค๋๋ค.
๊ทธ๋์ ๋์ ๋ณด์ด๋ ๋ฐ์ดํฐ๋ ๋๊ฐ์ง๋ง, hiObj์ helloObj๊ฐ ๊ฐ๊ณ ์๋ ์ง์ง ๊ฐ์ ๋ค๋ฅธ ๊ฒ์ ๋๋ค.console.log('๊ฐ์ฒด๋น๊ต =>', hiObj === helloObj); //false ๊ฐ์ฒด๋ ๋ค๋ฅด๊ณ console.log('๊ฐ์ฒด๊ฐ๋น๊ต =>', hiObj.name === helloObj.name); //true ๊ฐ์ฒด๊ฐ์ ๊ฐ๋ค
๊ทธ๋์ ๊ฐ์ฒด๋ฅผ ๋ด์ ๋ณ์๋ฅผ ๋น๊ตํ๋ฉด ์๋ก ๊ฐ์ง ์๋ค๊ณ ๋์ต๋๋ค. ๊ทธ๋ฌ๋ ๊ฐ์ฒด ๋ด๋ถ์ ํ๋กํผํฐ ๊ฐ์ด ํ ์คํธ์ผ ๊ฒฝ์ฐ๋, ํ ์คํธ๋ฅผ ๋น๊ตํ๊ฒ ๋์ด ์๋ก ๊ฐ์/๋ค๋ฆ ์ฌ๋ถ๋ฅผ ํ๋จํ ์ ์์ต๋๋ค.
const mutableObj = { name: '๊ฐ์ฒด' }; mutableObj = { name: '์์ ' } mutableObj.name = '์์ ๋ฉ๋๋ค!';
const๋ก ์ ์ธ๋ ๋ณ์๋ ์ ๋ ๊ฐ์ด ๋ฐ๋๋ฉด ์๋ฉ๋๋ค. ๊ทธ๋์ mutableObj์ ์๋ก์ด ๊ฐ์ฒด๋ฅผ ํ ๋นํ๋ฉด ์ค๋ฅ๊ฐ ๋ฉ๋๋ค. ์๋๋ฉด ์๋ก์ด ๋ฉ๋ชจ๋ฆฌ ์ฃผ์(reference)๋ก ์์ ์ ์๋ํ๊ธฐ ๋๋ฌธ์ด์ฃ .
ํ์ง๋ง mutableObj.name ๋ก ํ๋กํผํฐ๋ฅผ ์ ๊ทผํด์ ์์ ํ ์ ์์ต๋๋ค. mutableObj๊ฐ ์ ์ฅ๋ reference๊ฐ ๋ฐ๋๋ ๊ฒ์ด ์๋๋ผ ๊ฐ์ฒด ๋ด๋ถ์ ํ๋กํผํฐ ๊ฐ์ด ์์ ๋๋ ๊ฒ์ด๋ผ ์์ ์ด ๋๋ ๊ฒ ์ ๋๋ค!
๐คฒ Assignment
์๋์ ๊ฐ์ฒด์์ '์๋ฌ๋'๋ผ๋ ๊ฐ์ด ์ถ๋ ฅ ๋ ์ ์๋๋ก getAnswerํจ์์์ return ํด ์ฃผ์ธ์.let myProfile = { name: '๊น๊ฐ๋ฐ', address: { email: 'geabal@gmail.com', home: '์์ํฌ' }, 'my favorite': { food: [{ name: '์๋ฌ๋', price: 3500 }, { name: '์ผ๊ฒน์ด', price: 15000 }], hobby: ['์ถ๊ตฌ'] } }
function getAnswer () { return myProfile.['my favorite'].food[0].name; }
๊ฐ์ฒด
myProfile
์ ์ ๊ทผ -> ๊ฐ์ฒด 'my favorite' ์ ์ ๊ทผ (ํ๋ฐ์ดํ์ ๊ณต๋ฐฑ์ด ์๊ธฐ ๋๋ฌธ์ [] ๋ก ๊ฐ์ธ์ค๋ค.) -> ๊ฐ์ฒดfood
[0]์ ์ ๊ทผ (๋ฐฐ์ด ์์์ด๊ธฐ ๋๋ฌธ์ ์๋ฌ๋๊ฐ ์๋ ์ธ๋ฑ์ค๋ฅผ ์ง์ ํด์ค๋ค.) ->name
๊ฐ์ฒด์ ์ ๊ทผ! ์๋ฌ๋ ์ถ๋ ฅ.