1. function 축약
- 객체 데이터 내부에서 일반함수를 사용할 때 function 축약 가능
const ww = {
name: 'WW',
normal: function () {
console.log(this.name)
}
}
// 축약형
const ww = {
name: 'WW',
normal() {
console.log(this.name)
}
}
2. ES6 Classes
function User(first, last) {
this.firstName = first
this.lastName = last
}
User.prototype.getFullName = function () {
return `${this.firstName} ${this.lastName}`
}
const ww = new User('WW', 'Lee')
const kim = new User('Chulsoo', 'Kim')
const park = new User('Younghee', 'Park')
console.log(ww)
console.log(kim.getFullName())
console.log(park.getFullName())
class User {
constructor(first, last) {
this.firstName = first
this.lastName = last
}
getFullName() {
return `${this.firstName} ${this.lastName}`
}
}
3. 상속(확장)
class Vehicle {
constructor(name, wheel) {
this.name = name
this.wheel = wheel
}
}
const myVehicle = new Vehicle('운송수단', 2)
console.log(myVehicle)
// 상속(확장)
class Bicycle extends Vehicle {
constructor(name, wheel) {
// super : 부모 클래스의 인스턴스를 참조
super(name, wheel)
}
}
const myBicycle = new Bicycle('삼천리', 2)
const sonsBicycle = new Bicycle('세발', 3)
console.log(myBicycle)
console.log(sonsBicycle)
class Car extends Vehicle {
constructor(name, wheel, license) {
super(name, wheel)
this.license = license
}
}
const myCar = new Car('벤츠', 4, true)
console.log(myCar)
댓글남기기