추상화
캡슐화
자바스크립트에서 객체 캡슐화를 하는 건 처음이지만!
프로퍼티 뿐 아니라 함수도 캡슐화가 가능하다.
function createUser(email, birthdate){
const _email = email
let _point = 0
function increasePoint(){
_point +=1
}
const user = {
birthdate,
get email(){
return _email
},
get point(){
return _point
},
set email(address){
if(address.includes('@')){
_email = address
}else{
throw new Error('invalid email address')
}
},
buy(item){
console.log(`${this.email} buys ${item.name}`)
increasePoint()
}
}
return user
}
상속
class User{
constructor(email, birthdate){
this.email = email
this.birthdate = birthdate
}
buy(item){
console.log(`${this.email} buys ${item.name}`)
}
get email(){
return this._email
}
set email(address){
if(address.includes('@')){
this._email = address
}else{
throw new Error('invalid email address')
}
}
} //Parent Class
class PremiumUser extends User{
constructor(email, birthdate, level){
super(email, birthdate)
this.level = level
}
streamMusicForFree(){
console.log(`Free music streaming for ${this.email}`)
}
} //Child Class
다형성
class User{
constructor(email, birthdate){
this.email = email
this.birthdate = birthdate
}
buy(item){
console.log(`${this.email} buys ${item.name}`)
}
get email(){
return this._email
}
set email(address){
if(address.includes('@')){
this._email = address
}else{
throw new Error('invalid email address')
}
}
} //Parent Class
class PremiumUser extends User{
constructor(email, birthdate, level, point){
super(email, birthdate)
this.level = level
this.point = point
}
buy(item){
super.buy(item)
this.point += item.price * 0.05
}
streamMusicForFree(){
console.log(`Free music streaming for ${this.email}`)
}
} //Child Class