블로그 보관함

2014년 3월 4일 화요일

JavaScript 13 Factory Method를 이용한 객체 생성

<script>
"use strict";
//팩토리 메소드를 이용한 객체 생성 방법 개선
function makeStudent(name, kor, eng, math) {
return {
"name" : name,
"kor" : kor,
"eng" : eng,
"math" : math
};
}
// 객체배열 - makeStudent() 사용
var students = [ makeStudent('홍길동', 100, 90, 80),
makeStudent('임꺽정', 90, 95, 100),
makeStudent('장길산', 50, 90, 65) ];
console.log('forEach----------------------------------------')
students.forEach(function(parameter) {
console.log(parameter.name, parameter.kor, parameter.eng,
parameter.math);
});
// Solution
console.log('Solution-----------------------------------------')
students.forEach(function(student, index, array) {
console.log(index, array, student, student.name, student.kor,
student.eng, student.math);
});
</script>

댓글 없음:

댓글 쓰기