JAVASCRIPT
class
걍가영
2020. 7. 30. 11:57
class
생성자의 기능을 대체
- 함수로 호출될 수 없음
- 클래스 선언은 let과 const처럼 블록 스코프에 선언, 호이스티잉 일어나지 않음
- 클래스의 ㅔ소드 안에서 super 키워드 사용
// 클래스
class Person {
// 이전에서 사용하던 생성자 함수는 클래스 안에 `constructor`라는 이름으로 정의
constructor({name, age}) {
this.name = name;
this.age = age;
}
// 객체에서 메소드를 정의할 때 사용하던 문법을 그대로 사용하면, 메소드가 자동으로 `Person.prototype`에 저장
introduce() {
return `안녕하세요, 제 이름은 ${this.name}입니다.`;
}
}
const person = new Person({name: '윤아준', age: 19});
console.log(person.introduce()); // 안녕하세요, 제 이름은 윤아준입니다.
console.log(typeof Person); // function
console.log(typeof Person.prototype.constructor); // function
console.log(typeof Person.prototype.introduce); // function
console.log(person instanceof Person); // true