JS Classes

내가해냄·2023년 2월 10일
0

JS

목록 보기
5/8
  • new 키워드로 생성
class Person{
	name = 'Max' # property
	call = () => {...} # method
}

const myPerson = new Person()
myPerson.call()
console.log(myPerson.name)
  • 상속 (Inheritance)
    다른 클래스에 있는 플로퍼티와 메소드를 상속하면 잠재적으로 새로운 프로퍼티와 메소드를 추가함
class Person extends Master

class Human { 
  constructor() { 
    this.gender = 'male';
  }
  
  printGender() {
    console.log(this.gender);
  }
  
}

class Person extends Human {
  #다른 클래스를 확장하고 생성자를 실행하면 super키워드를 추가해야함
  constructor() { 
    super(); # 상위클래스의 생성자 함수를 실행함
    this.name = 'Max';
  }
  
  printMyname() {
    console.log(this.name);
  }
}

const person = new Person();
person.printMyname();
person.printGender();

최신구문

ES6

constructor() {
	this.myProperty = 'value'
}

myMethod() {...}

ES7

myProperty = 'value'

myMethod = () => {...}

생성자함수 필요 X
화살표 함수 -> this 키워드 사용하지 않아도 됨


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();
profile
노션으로 갈아탐

0개의 댓글