본문 바로가기
프론트엔드/JavaScript

ES6 Class

by step 1 2021. 5. 28.
반응형

기존 문법을 ES6 Class로 바꾸는 예제

// ES6 Classes
const happy = {
    name: 'happy',
    normal(){
        console.log(this.name)
    },
    arrow: () => {
        console.log(this.name)
    }
}

happy.normal()
happy.arrow()

// 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 happy2 = new User('Happy', 'Oh')
const amy = new User('amy', 'Clack')
const neo = new User('Neo', 'Smith')

console.log(happy2)
console.log(amy.getFullName())
console.log(neo.getFullName())

반응형

'프론트엔드 > JavaScript' 카테고리의 다른 글

JS 데이터 - 문자  (0) 2021.05.28
상속(확장)  (0) 2021.05.28
this  (0) 2021.05.28
생성자 함수 (prototype)  (0) 2021.05.27
콜백 함수  (0) 2021.05.26