<script type="text/javascript">
        function makeStudent(name, korean, math, english, science) {
            var willReturn = {
            // 속성
                이름: name,
                국어: korean,
                수학: math,
                영어: english,
                과학: science,
            // 메서드
                getSum: function () {
                    return this.국어 + this.수학 + this.영어 + this.과학;
                },
                getAverage: function () {
                    return this.getSum() / 4;
                },
                toString: function () {
                    return this.이름 + '\t' + this.getSum() + '\t' + this.getAverage();
                }
            };
            return willReturn;
        }

        // 학생 정보 배열
        var students = [];
        students.push(makeStudent('태연', 1, 6, 11, 16));
        students.push(makeStudent('수지', 2, 7, 12, 17));
        students.push(makeStudent('써니', 3, 8, 13, 18));
        students.push(makeStudent('서현', 4, 9, 14, 19));
        students.push(makeStudent('소희', 5, 10, 15, 20));

        // 출력합니다.
        var output = '이름\t총점\t평균\n';
        for (var i in students) {
            output += students[i].toString() + '\n';
        }
        alert(output);

</script>

 

    makeStudent 함수로 찍어내듯 객체를 생성할 수 있다. 하지만 생성자 함수를 사용하면  


    기능이 많은 객체를 쉽게 만들 수 있기 때문에 잘 사용하지 않는다


'개발개발 > 자바스크립트' 카테고리의 다른 글

프로토타입  (0) 2012.08.27
생성자 함수  (0) 2012.08.27
객체와 배열을 사용한 데이터 관리  (0) 2012.08.27
객체 속성 추가(동적)  (0) 2012.08.27
with 키워드  (0) 2012.08.27
Posted by 공돌공돌
,