09.html-생성자 함수를 이용한 객체 생성

CODEDRAGON Development/JavaScript, jQuery, ...

반응형

생성자 함수를 이용한 객체 생성 2

   

   

소스 코드

   

  1. <script type="text/javascript">
  2.         //생성자 함수
  3.         function Student(name, korean, math, english, science){
  4.                 //속성 지정
  5.                 //this 전역변수로 this없으면 지역변수
  6.                 this.name = name;
  7.                 this.korean = korean;
  8.                 this.math = math;
  9.                 this.english = english;
  10.                 this.science = science;
  11.                  
  12.                 //메소드 지정
  13.                 this.getSum = function(){
  14.                         return this.korean + this.math + this.english + this.science;
  15.                 };
  16.                 this.getAverage = function(){
  17.                         return this.getSum()/4;
  18.                 };
  19.                 this.toString = function(){
  20.                         return this.name + ', ' + this.getSum() + ', ' + this.getAverage();
  21.                 };             
  22.         }
  23.          
  24.         //생성자 함수를 이용한 객체 생성
  25.         var student = new Student('홍길동', 90, 77, 80, 95);
  26.          
  27.         //출력
  28.         document.write(student);
  29.          
  30.          
  31.         document.write('<br>');
  32.         //생성자 함수를 이용한 객체 생성
  33.         var student2 = new Student('대장금', 53, 87, 86, 67);       
  34.         document.write(student2);
  35.          
  36.          
  37. </script>
  38. </head>
  39. <body>
  40.    
  41. </body>


   

 

출력결과

   

반응형