비슷한 객체를 사용하기 위해서 생성자 함수를 사용해왔다
const User= function (name, age){
this.name=name;
this.age=age;
this.showName = function() {
console.log(this.name);
};
};
const mimi= new User("mimi", 20);
위 생성자 함수를 클래스로 만들어 보자
class User2 {
constructor(name, age) {
this.name = name;
this.age= age;
}
showName() {
console.log(this.name);
}
}
const riri= new User("riri", 18);
클래스로 만들면 사용법이 동일한 것 같지만 클래스로 만들면 쇼네임이 프로토 타입에 저장되어 있다(유일한 차이).
프로토타입을 활용해서 추가하면 동일하게 만들 수 있다.
'JavsScript' 카테고리의 다른 글
class ; 메소드 오버라이딩 (0) | 2022.10.02 |
---|---|
Class ; 상속 (0) | 2022.10.02 |
상속, prototype (0) | 2022.02.26 |
call, apply,bind (0) | 2022.02.26 |
setTimeout/SetInterval (0) | 2022.02.25 |