Javascript&Jquery
생성자 함수_new를 이용한 객체생성
알 수 없는 사용자
2012. 1. 23. 17:23
function Student(name, korean, math, english, science) {
this.이름=name;
this.국어=korean;
this.수학=math;
this.영어=english;
this.과학=science;
//매서드
this.getSum=function(){
return this.국어+this.수학+this.영어+this.과학;
};
this.getAverage=function(){
return this.getSum()/4;
};
this.toString=function(){
return this.이름+'\t'+this.getSum()+'\t'+this.getAverage();
};
}
//학생정보배열
var students=[];
students.push(new Student('윤하린',96,98,97,94));
students.push(new Student('체스터',95,96,98,45));
//출력
var output='이름\t총점\t평균\n';
for (var i in students){
output += students[i].toString() + '\n';
}
alert(output);