7-1. 레코드 캡슐화하기
Last updated
Last updated
const obj = {
peanut: {
age: 26,
department: '개발',
food: '국밥',
grades: {
2020: {
1: {
computerSicence: 'C'
}
}
}
},
Walnut: {
age: 25,
department: '디자인',
food: '피자',
grades: {
2020: {
1: {
computerSicence: 'C'
}
}
}
}
}
// 데이터 쓰는 경우
obj[nickname].grades[2020][1] = 'D';
// 데이터 읽는 경우
const gradeToPoint = (nickname, year, month) => {
const grade = obj[nickname].grades[year][month].computerScience;
if(grade === 'A') {
return 100;
} ...
}class UserData {
cosntructor(data) {
this._data = data
}
setGrade({ customerId, year, month, grade }) {
this._data[customerId].grades[year][month].computerScience = grade;
}
getGradeToPoint({ customerId, year, month }) {
const grade = this._data[customerId].grades[year][month].computerScience;
if(grade === 'A') {
return 100;
} ...
}
}
const obj = new UserData({
peanut: {
age: 26,
department: '개발',
food: '국밥',
grades: {
2020: {
1: {
computerSicence: 'C'
}
}
}
},
Walnut: {
age: 25,
department: '디자인',
food: '피자',
grades: {
2020: {
1: {
computerSicence: 'C'
}
}
}
}
})
// 데이터 쓰는 경우
obj.setGrade({ customerId, year, month, grade });
// 데이터 읽는 경우
obj.getGradeToPoint({ customerId, year, month });