TIL. 13 JavaScript - Object(๊ฐ์ฒด)

Minjae Choiยท2021๋…„ 5์›” 24์ผ
0

JavaScript

๋ชฉ๋ก ๋ณด๊ธฐ
3/20

๐ŸŽˆ ๊ฐ์ฒด ์ด๋ก ๊ณผ ํ™œ์šฉ

  • 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')๋Š” ๋ฉ”์†Œ๋“œ์ด๋‹ค.

0๊ฐœ์˜ ๋Œ“๊ธ€