object

기혁·2023λ…„ 3μ›” 6일
0

Javascript ν•™μŠ΅

λͺ©λ‘ 보기
13/15

day08

πŸ“Œ javascript 객체생성 κ³Όμ •

<script>
        /*
            javascript 객체생성 κ³Όμ •
            - 빈 객체의 생성
                -> μ•„λ¬΄λŸ° κΈ°λŠ₯이 μ—†λŠ” μƒνƒœμ˜ 빈 객체λ₯Ό 생성
                이 μƒνƒœκ°€ prototype 인닀.
            - λ³€μˆ˜μ˜ μΆ”κ°€
            - ν•¨μˆ˜μ˜ μΆ”κ°€

            빈 객체 생성
            - let people = {};

            λ³€μˆ˜μ˜ μΆ”κ°€
            - people.name = "μžλ°”ν•™μƒ";
            - people.gender = "μ—¬μž";
        */

        let people = {};

        // κ°μ²΄μ•ˆμ— λ³€μˆ˜(λ©€λ²„λ³€μˆ˜, ν”„λ‘œνΌν‹°)
        people.name = "μžλ°”ν•™μƒ";
        people.gender = "μ—¬μž";

        document.write("<h1>" + people.name + " λ‹˜μ€ "
                + people.gender + " μž…λ‹ˆλ‹€</h1>");

    </script>

πŸ’‘ κ²°κ³Όκ°’
people.name = μžλ°”ν•™μƒ
people.gender = μ—¬μž

πŸ“Œ javascript 객체생성 κ³Όμ • (2)

	<script>
        let grades = {
            'abc' : 10,
            'def' : 6,
            'ghr' : 80
        };

        // document.write(grades['abc'] + "<br>");
        // document.write(grades.def + "<br>");

        for( key in grades ){
            document.write("<li>key : " + key 
                + " ,value : " + grades[key] + "</li>");
        }
        
    </script>

πŸ’‘ κ²°κ³Όκ°’

πŸ“Œ 객체이름.ν•¨μˆ˜μ΄λ¦„ = fucntion(νŒŒλΌλ―Έν„°)

	<script>
        /*
            객체이름.ν•¨μˆ˜μ΄λ¦„ = fucntion(νŒŒλΌλ―Έν„°){
                .. ν•¨μˆ˜ κ΅¬ν˜„ λΆ€λΆ„..
                return κ°’;
            };

            - λ©”μ„œλ“œ μ•ˆμ—μ„œ 객체 μžμ› ν™œμš©ν•˜κΈ°
            - 객체 μ•ˆμ— ν¬ν•¨λœ λ©”μ„œλ“œμ—μ„œ λ‹€λ₯Έ λ©”μ„œλ“œλ₯Ό
            ν˜ΈμΆœν•˜κ±°λ‚˜, ν”„λ‘œνΌν‹°(λ©€λ²„λ³€μˆ˜)λ₯Ό ν™œμš©ν•˜κ³ μž
            ν•˜λŠ” κ²½μš°μ—λŠ” this ν‚€μ›Œλ“œλ₯Ό μ‚¬μš©ν•œλ‹€.
            
            this.λ³€μˆ˜μ΄λ¦„;
        */
        let people = {};

        people.name = "μžλ°”ν•™μƒ";
        people.gender = "λ‚¨μž";

        // ν•¨μˆ˜λ₯Ό ν¬ν•¨μ‹œν‚€κΈ°
        people.sayName = function(){
            document.write("<h1>" + this.name + "</h1>");
        };

        people.sayGender = function(){
            document.write("<h1>" + this.gender + "</h1>");
        };

        people.saySomething = function(msg){
            document.write("<h1>" + msg + "</h1>");
        };

        people.getName = function(){
            return this.name;
        };

        people.getGender = function(){
            return this.gender;
        };

        people.sayInfo = function(){
            document.write("<h1>" + this.getName()
                + "λ‹˜μ€ " + this.getGender() + "μž…λ‹ˆλ‹€.</h1>");
        };

        // κ°μ²΄μ•ˆμ— λ©”μ„œλ“œ 호좜
        people.sayName();
        people.sayGender();
        people.saySomething("Hello javascript");
        people.sayInfo();
    </script>

πŸ’‘ κ²°κ³Όκ°’
people.sayName(); = μžλ°”ν•™μƒ
people.sayGender(); = λ‚¨μž
people.saySomething(msg) = // msg에 νŒŒλΌλ―Έν„° νˆ¬μž…ν•  λ‚΄μš© (좜λ ₯ν•  λ‚΄μš©)
people.saySomething("Hello javascript");
people.sayInfo(); = μžλ°”ν•™μƒλ‹˜μ€ λ‚¨μžμž…λ‹ˆλ‹€.

		<script>
            // let person = {};
            // person.name = 'kjh';
            // person.introduce = function() {
            //     return 'My name is ' + this.name;
            // }

            // document.write(person.introduce());

            let person = {
                'name'      : 'kjh2',
                'introduce' :   function(){
                    return 'My name is ' + this.name;
                }
            }

            document.write(person.introduce());
        </script>

πŸ’‘ κ²°κ³Όκ°’

πŸ“Œ let person = {};

		<script>
            // let person = {};
            // person.name = 'lkh';
            // person.introduce = function() {
            //     return 'My name is ' + this.name;
            // }

            // document.write(person.introduce());

            let person = {
                'name'      : 'lkh2',
                'introduce' :   function(){
                    return 'My name is ' + this.name;
                }
            }

            document.write(person.introduce());
        </script>

πŸ’‘ κ²°κ³Όκ°’

profile
β­οΈλ‚΄κ°€λ§Œλ“ μΏ ν‚€β­οΈ

0개의 λŒ“κΈ€