앞서 알아봤던 생성자 함수를 class라는 키워드를 통해서 간편하게 바꾸는 코드를 알아 볼 것이다.
// function User(first, last) {
// this.firstName = first
// this.lastName = last
// }
// User.prototype.getFullName = function () {
// return `${this.firstName} ${this.lastName}`
// }
class User {
constructor(first, last) {
this.firstName = first
this.lastName = last
}
getFullName() {
return `${this.firstName} ${this.lastName}`
}
}
const flyda = new User('Flyda' , 'Yang')
const amy = new User('Amy', 'hwang')
const neo = new User ('Neo' , 'Lee')
console.log(flyda)
console.log(amy.getFullName())
console.log(neo.getFullName())
앞으로는 생성자 함수를 작성할 때 다음과 같은 방식으로 작성해야한다.
'핀테크 서비스 프론트엔드 개발자 취업 완성 2기 > JS' 카테고리의 다른 글
[JS] 데이터형(dataType), 변수선언 , 함수(function) (0) | 2022.04.18 |
---|---|
[JS] 상속(확장) (0) | 2022.04.15 |
[JS] this (0) | 2022.04.15 |
[JS] 클래스 - 생성자 함수 (prototype) (0) | 2022.04.15 |
[JS] 데이터 타입 확인 함수 (0) | 2022.04.14 |
댓글