class Person{
constructor(name, first, second){
this.name = name;
this.first = first;
this.second = second;
}
sum(){
return this.first + this.second
}
}
let kim = new Person('kim', 10, 20);
kim.sum = function(){
return this.first * this.second
}
console.log(kim)
console.log(kim.sum())
๐ constructor(){}
๐ class์์ ๋ฉ์๋
class Person{
constructor(name, first, second){
this.name = name;
this.first = first;
this.second = second;
}
sum(){
return this.first + this.second
}
}
class PersonPlus extends Person {
avg(){
return (this.first+this.second)/2;
}
}
let kim = new PersonPlus('kim', 10 ,20)
console.log(kim.sum())
console.log(kim.avg())
๐ class PersonPlus extends Person{}
โช ์์์ ์ ํ์ํ ๊น?
class Person{
constructor(name, first, second){
this.name = name;
this.first = first;
this.second = second;
}
sum(){
return this.first + this.second
}
}
class PersonPlus extends Person {
constructor(name, first, second, third){
super(name, first, second);
this.third = third;
}
sum(){
return super.sum()+this.third;
}
avg(){
return ( this.first+this.second+this.third )/3;
}
}
let kim = new PersonPlus('kim', 10 ,20, 30)
console.log(kim.sum())
console.log(kim.avg())
๐super(): ๋ถ๋ชจ class์ ์์ฑ์(constructor)๋ฅผ ํธ์ถ
๐super. : ๋ถ๋ชจํด๋์ค์ ๋ฉ์๋ ํธ์ถ