본문 바로가기
JavaScript(JS)

JS03

by EUN-JI 2023. 11. 2.
<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>prototype</title>
    </head>

    <body>
        <!-- js객체는 모든 멤버를 객체마다 보유하고 있음.
            그래서 같은 코드가 작성되어 있는 멤버함수도 객체마다 존재 -->
            <!-- 같은 객체들이 하나의 함수를 공유하여 사용하는 기술   -프로토타입-->
            <script>
                //생성자함수 = java의 class같은 역할[객체의 설계도]
                function Person(name, age){
                    this.name= name;
                    this.age= age;
                    }
                    //Person 자료형에 하나만 존재하는 메소드 추가
                    Person.prototype.show= function(){
                        document.write("Person : " +this.name+" , "+this.age+"</br>");
                    }

                    //객체 생성
                    var p1= new Person("sam",20);
                    var p2= new Person("robin",25);

                    //객체의 멤버를 출력하는 프로토타입 메소드들 사용해보기.
                    p1.show();
                    p2.show();
       
               
            </script>
    </body>
</html>

'JavaScript(JS)' 카테고리의 다른 글

JS05  (1) 2023.11.03
JS04  (0) 2023.11.02
JS02  (1) 2023.11.02
JS01  (1) 2023.10.31
JS//01  (0) 2023.08.30