자바스크립트의 class는 객체와 관련이 있다.
객체를 직접 작성하여 정의하고 생성할 수도 있지만, 클래스로 만들어주면 여러 객체를 더 쉽게 만들 수 있다.
클래스는 객체를 생성하기 위한 템플릿(설계도)이다.
한마디로 클래스는 = 붕어빵 기계, 그리고 객체 = 붕어빵
class parent{
constructor(name,age){
this.name=name
this.age=age
}
}
const child = new parent();
class parent{
constructor(name,age){
this.name=name
this.age=age
this.sayHi = function(){...}
}
}
const child = new parent('Park',30);
혹은 prototype에 추가하려면..
class parent{
constructor(name,age){
this.name=name
this.age=age
}
sayHi(){
console.log('hello')
}
}
const child = new parent();