예전 방식
function Vacation(destination, length){
this.destination = destination
this.length = length
}
Vacation.prototype.print = function(){
console.log(this.destination + "는 "+
this.length)
}
var trip = new Vacation("대구",4)
trip.print()
<출력 결과>
"대구는 4"
최신 방식
class Vacation{
constructor(destination, length){
this.destination = destination
this.length = length
}
print(){
console.log(`${this.destination}은(는)${this.length}일 걸립니다.`)
}
}
var trip = new Vacation("강남",2)
trip.print()
<출력 결과>
"강남은(는)2일 걸립니다."
상속 기능
class Vacation{
constructor(destination, length){
this.destination = destination
this.length = length
}
print(){
console.log(`${this.destination}은(는)${this.length}일 걸립니다.`)
}
}
var trip = new Vacation("강남",2)
trip.print()
class Expedition extends Vacation{
constructor(destination, length, gear){
super(destination,length)
this.gear = gear
}
print(){
super.print()
console.log(`추가 ${this.gear}`)
}
}
var upTrip = new Expedition("강남",2,10)
upTrip.print()
<출력 결과>
"강남은(는)2일 걸립니다."
"강남은(는)2일 걸립니다."
추가 10"
'Development > JavaScript' 카테고리의 다른 글
| JavaScript 비동기 처리 방식 (0) | 2023.09.04 |
|---|---|
| JavaScript 바인딩 (0) | 2023.09.02 |
| 문자열 연결, 호이스팅 (0) | 2023.09.02 |
| JavaScript문법 Destructuring 구조분해 할당 (0) | 2023.01.15 |
| JavaScript 스프레드 연산자, 나머지 연산자, === 연산자 (0) | 2023.01.15 |