class Person {
name = 'Max';
call = () => { ... }
}
const myPerson = new person(); // 클래스 선언
myPerson.call(); // 함수 사용
console.log(myPerson.name); // 변수 사용
class Person extends Master
class Human{
constructor(){
this.gender = 'male';
}
printGender(){
console.log(this.gender);
}
}
class Person extends Human {
constructor(){
super(); // super()을 사용해 줌으로써 상속하고 있는 Human 클래스를 초기화해준다.
this.name = 'Max';
this.gender = 'female';
}
printMyName(){
console.log(this.name);
}
}
const person = new Person();
person.printMyName();
person.printGender();
리액트에서 컴포넌트를 생성할 때 위와 같은 방식으로 사용되며, 이는 컴포넌트를 생성하는 두 가지 방식 중 하나이다.
class Human {
gender = 'male';
printGender = () => {
console.log(this.gender);
}
}
class Person extends Human{
name = 'Max';
gender = 'female';
printMyName(){
console.log(this.name);
}
}
const person = new Person();
person.printMyName();
person.printGender();
일반 자바스크립트를 ES6 자바스크립트로 변경해주면 위와 같다.
유데미 강의 참고
Maximilian Schwarzmüller - React 완벽 가이드 with Redux, Next.js, TypeScript