ES6 방식의 상속기능 (class)

정중식·2023년 2월 18일
0

자바스크립트

목록 보기
12/20

class란?

  • 자바스크립트의 class는 객체와 관련이 있다.

  • 객체를 직접 작성하여 정의하고 생성할 수도 있지만, 클래스로 만들어주면 여러 객체를 더 쉽게 만들 수 있다.

  • 클래스는 객체를 생성하기 위한 템플릿(설계도)이다.

  • 한마디로 클래스는 = 붕어빵 기계, 그리고 객체 = 붕어빵

class로 constructor 만들기

class parent{
	constructor(name,age){
      this.name=name 
      this.age=age
    }
}

const child = new parent();

상속가능한 함수 추가

class parent{
	constructor(name,age){
      this.name=name 
      this.age=age
      this.sayHi = function(){...}
    }
}

const child = new parent('Park',30);

혹은 prototype에 추가하려면..

class parent{
	constructor(name,age){
      this.name=name 
      this.age=age
    }
  sayHi(){
  	console.log('hello')
  }
}

const child = new parent();
profile
내 가치를 찾아서

0개의 댓글