클래스 정의
const Person = class = {}
const Person = class MyClass{}
- 클래스를 표현식으로 정의할 수 있다 = 클래스가 값으로 사용할 수 있다 =
일급객체
- 무명의 리터럴로 생성할 수 있다. 즉, 런타임에 생성이 가능하다
- 변수나 자료구조(객체, 배열 등)에 저장할 수 있다
- 함수의 매개변수에게 전달할 수 있다
- 함수의 반환값으로 사용할 수 있다
- 클래스 몸체에서 정의할 수 있는 메서드는 constructor(생성자), 프로토타입 메서드, 정적메서드 세가지가 있다
클래스와 생성자 함수의 정의 방식 비교
생성자함수
var Person = (function() {
function Person(name) {
this.name = name;
}
Person.prototype.sayHi = function() {
console.log('Hi! My name is' + this.name);
}
Person.sayHello = function() {
console.log('Hello!');
}
return Person;
}());
클래스
class Person {
constructor(name) {
this.name = name;
}
sayHi() {
console.log('Hi! My name is ${this.name}');
}
static sayHello() {
console.log('Hello!');
}
}