ES6 Classes
- 일반함수인
nomal: function()
을 normal()
로 축약
const heropy = {
name: 'Heropy',
normal() {
console.log(this.name)
},
arrow: () => {
console.log(this.name)
}
}
heropy.normal()
heropy.arrow()
prototype
으로 작성된 코드를 es6 class를 활용해 축약
function User(first, last) {
this.firstName = first
this.lastName = last
}
User.prototype.getFullName = function () {
return `${this.firstName} ${this.lastName}`
}
const heropy = new User('Heropy', 'Park')
const amy = new User('Amy', 'Clarke')
const neo = new User('Neo', 'Smith')
console.log(heropy)
console.log(amy.getFullName())
console.log(neo.getFullName())
class User {
constructor(first, last) {
this.firstName = first
this.lastName = last
}
getFullName() {
return `${this.firstName} ${this.lastName}`
}
}
const heropy = new User('Heropy', 'Park')
const amy = new User('Amy', 'Clarke')
const neo = new User('Neo', 'Smith')
console.log(heropy)
console.log(amy.getFullName())
console.log(neo.getFullName())