1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
<script>
function Student(name, korean, math, english) {
this.이름 = name;
this.국어 = korean;
this.수학 = math;
this.영어 = english;
this.getSum = function () {
return this.국어 + this.수학 + this.영어;
};
this.getAverage = function () {
return this.getSum() / 4;
};
this.toString = function () {
return this.이름 + '\t' + this.getSum() + '\t' + this.getAverage();
};
}
var student = new Student('멍개', 100, 100, 100);
alert(student);
</script>
| cs |
Student 생성자 함수를 사용해 객체를 생성하면 해당 객체는 이름, 국어, 수하, 영어 속성과 getSum(), getAerage(), toString()메서드를 갖는다.
생성자 함수를 사용해 객체를 사용해 객체를 생성할 때에는 new키워드를 사용.

출력은 잘... 되긴하는데 가운데 사각형은 뭘까???????? 머지... ㅋㅋㅋㅋㅋㅋㅋ
**생성자 함수의 이름은 일반적으로 대문자로 시작을 한다.
1
2
3
4
5
6
7
8
9
10
11
|
var student=[];
student.push(new Student('멍개', 100, 100, 100));
student.push(new Student('짱수', 90, 90, 90));
var output='';
for (var i in student) {
output += student[i].toString() + '\n';
}
alert(output);
| cs |

댓글
댓글 쓰기