[코어 자바스크립트] Class

소이뎁·2023년 7월 10일
0
post-thumbnail

🌈 인프런의 코어 자바스크립트(정재남) 수강 후, 이해한 내용을 정리한 글입니다.

Class는 특정 객체를 생성하기 위해 변수와 메소드를 정의하는 일종의 틀이다.

아래 그림에서 빨간 박스 부분을 의미한다.

프로퍼티가 정의된 곳에 따라 static과 prototype으로 나뉜다.

constructor에 정의된 것은 static, prototype에 직접 정의된 것은 prototype로 분류할 수 있다. 화살표에서 알 수 있듯이 instance에서 static methods, static properties의 접근은 불가능하다. 반면 instance에서 (prototype) methods의 접근은 가능하다.

예를 들면

Array(constructor) 안의 methods와 properties는 각각 static methods, static properties이고, prototype 내부의 methods는 (prototype) methods이다.

// static methods는 instance에서 직접 접근 불가능[1, 2, 3].isArray()
✅ Array.isArray([1, 2, 3])

// (protytpe) methods는 instance에서 직접 가능[1, 2, 3].pop()
❌ Array.pop([1, 2, 3])

3줄 요약

  • class는 특정 객체를 생성하기 위해 변수와 메소드를 정의하는 일종의 틀이다.
  • constructor에 정의된 것은 static methods/static properties, prototype에 정의된 것은 (prototype) methods로 불린다.
  • instance → static methods/static properties는 불가능, instance → (prototype) methods 가능하다.

0개의 댓글