Object(๊ฐ์ฒด)
ํจ์์ ์๋ก ์ฐ๊ด๋ ๋ณ์๋ฅผ ๊ฐ์ ์ด๋ฆ์ผ๋ก ๊ทธ๋ฃนํํ๊ธฐ ์ํ ๋๊ตฌ์ด๋ค.
๋ฉ์๋์ ํ๋กํผํฐ
๊ฐ์ฒด์ ํฌํจ๋ ํจ์๋ ๋ฉ์๋(method), ๋ณ์๋ ํ๋กํผํฐ(property)๋ผ๊ณ ๋ถ๋ฅธ๋ค.
๊ฐ์ฒด์ property ๋ผ๋ฆฌ๋ฅผ ๊ตฌ๋ถํ ๋๋ ์ฝค๋ง(,)๋ฅผ ์ฌ์ฉํ๋ค.
JavaScript ํ์ผ์ ๋ฆฌ
css์ ๋ง์ฐฌ๊ฐ์ง๋ก, script ํ๊ทธ ์์ ๋ด์ฉ์ .js ํ์ฅ์ ํ์ผ๋ก ๋ฐ๋ก ์ ๋ฆฌํ ์ ์์ผ๋ฉฐ, <script src="ํ์ผ๋ช
.js"></script>
์ฝ๋๋ก ์ฐ๊ฒฐํ๋ค.
๊ฐ์ฒด๋ ์ค๊ดํธ{}๋ฅผ ์ฌ์ฉํ๋ฉฐ, ์์๊ฐ ์๋ ๋ฐฐ์ด๊ณผ๋ ๋ฌ๋ฆฌ ์์๊ฐ ์๋ค.
* ์ฒซ ๋ฒ์งธ ๋ฐฉ๋ฒ(์ผ๋ฐ ๊ฐ์ฒด)
<script>
var coworkers = {
"programmer":"egoing",
"designer":"leezche"
};
document.write("programmer : "+coworkers.programmer+"<br>");
document.write("designer : "+coworkers.designer+"<br>");
coworkers.bookeeper = "duru";
document.write("bookeeper : "+coworkers.bookeeper+"<br>");
coworkers["data scientist"] = "taeho";
document.write("data scientist : "+coworkers["data scientist"]+"<br>");
</script>
// coworkers๋ผ๋ ๊ฐ์ฒด์ ์์ฑ๊ณผ ๊ทธ ์์ ๋ด์ฉ๋ค์ ๊ฐ์ ธ์ค๋ ๋ฐฉ๋ฒ, ๊ทธ๋ฆฌ๊ณ ๋ด์ฉ ์ถ๊ฐ ๋ฐฉ๋ฒ
------------------------------------------------------------------------------
* ๋ ๋ฒ์งธ ๋ฐฉ๋ฒ(๊ฐ์ฒด์ ๋ฐ๋ณต๋ฌธ ํ์ฉ)
<script>
for(var key in coworkers) {
document.write(key+' : '+coworkers[key]+'<br>');
}
</script>
// for(var key in coworkers) :
coworkers๋ผ๋ ๋ณ์๊ฐ ๊ฐ๋ฆฌํค๋ ๊ฐ์ฒด ์์ ์๋ key ๊ฐ๋ค์ ๊ฐ์ ธ์ค๋ ๋ฐ๋ณต๋ฌธ
------------------------------------------------------------------------------
* ์ธ ๋ฒ์งธ ๋ฐฉ๋ฒ(
<script>
var Links = {
setColor:function (color) {
var alist = document.querySelectorAll('a');
var i = 0;
while(i < alist.length) {
alist[i].style.color = color;
i = i + 1;
}
}
}
var Body = {
setColor:function (color) {
document.querySelector('body').style.color = color;
},
setBackgroundColor:function (color) {
document.querySelector('body').style.backgroundColor = color;
}
}
function nightDayHandler(self){
var target = document.querySelector('body');
if (self.value === 'night') {
Body.setBackgroundColor('black');
Body.setColor('white');
self.value = 'day';
Links.setColor('powderblue');
} else {
Body.setBackgroundColor('white');
Body.setColor('black');
self.value = 'night';
Links.setColor('blue');
}
}
</script>
// ์์์ Links์ Body๋ฅผ ๊ฐ์ฒด๋ก ์์ฑํ๊ณ ,
ํ๋กํผํฐ๋ก setColor๋ฅผ ์ง์ ํ ํจ์๋ฅผ ํ์ฉํด color๊ฐ์ ๋ณ๊ฒฝํ๋ค.
// Body.setColor('white')์์ Body๋ ๊ฐ์ฒด, setColor('white')๋ ๋ฉ์๋์ด๋ค.