Skip to content

25. 클래스 #132

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions 9주차 (25장)/25. 클래스/Sun4-me/example.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
//주의!! 서브클래스에서 constructor를 생략하지 않는 경우 서브클래스의 constructor에서는 반드시 super를 호출해야 한다.

// 옳바른 super사용
// 슈퍼클래스
class Base {
constructor(a, b) {
// ④
this.a = a;
this.b = b;
}
}

// 서브클래스
class Derived extends Base {
constructor(a, b, c) {
// ②
super(a, b); // ③
this.c = c;
}
}

const derived = new Derived(1, 2, 3); // ①
console.log(derived); // Derived {a: 1, b: 2, c: 3}

// 잘못된 super사용
class Base {}

class Derived extends Base {
constructor() {
// ReferenceError: Must call super constructor in derived class before accessing 'this' or returning from derived constructor
console.log("constructor call");
}
}

const derived = new Derived();
32 changes: 32 additions & 0 deletions 9주차 (25장)/25. 클래스/Sun4-me/기억에 남는 내용.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
## ✏️ 기억에 남는 내용

- 클래스는 `syntatic sugar`라고 보기보다는 새로운 객체 생성 메커니즘으로 보는 것이 합당하다.

- 클래스 호이스팅

- 프로토타입과 생성자 함수는 언제나 쌍으로 존재한다. 클래스는 클래스 정의 이전에 참조할 수 없다. 클래스 선언문 이전에 일시적 사각지대에 빠지기 때문에, 호이스팅이 발생하지 않는 것처럼 동작한다.

- 메서드

- 클래스는 인스턴스를 생성하기 위한 생성자 함수다. constructor를 생략하면 암묵적으로 정의된다. constructor 안에서 인스턴스 생성과 초기화를 담당하고 있기 때문에, 인스턴스를 초기화할때는 contructor를 생략해서는 안된다.

- 정적 메서드와 프로토타입 메서드의 차이

- 둘은 자신이 속해있는 프로토타입 체인이 다르다.
- 정적 메서드는 클래스로 호출하고 인스턴스 프로퍼티를 참조할 수 없다.
- 프로토타입 메서드는 인스턴스로 호출하고 인스턴스 프로퍼티를 참조할 수 있다.
- this를 사용하지 않는 메서드는 정적 메서드로 정의하는 것이 좋다.

- 프로퍼티

- 인스턴스 프로퍼티는 언제나 public하다. 자바스크립트는 private 키워드와 같은 접근 제한자를 지원하지 않는다. 대신 `#`를 이용해서 private 필드를 정의할 수 있다. 단, prevate 필드는 반드시 클래스 몸체에 정의해야 한다.

- 상속

- extends 키워드는 클래스뿐만 아니라 생성자 함수도 상속받을 수 있다. [[Construct]] 내부 메서드를 갖는 함수 객체로 평가될 수 있는 모든 표현식을 사용할 수 있다. 동적으로 정할 수도 있다.

- super 키워드 사용 주의사항

- 서브클래스애서 constructor를 사용한다면 반드시 super를 호출해야한다.
- constructor에서 super를 호출하기 전에는 this를 참조할 수 없다.
- super 키워드는 서브클래스의 constructor에서만 호출한다.
5 changes: 5 additions & 0 deletions 9주차 (25장)/25. 클래스/Sun4-me/퀴즈.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
## 📝 간단한 퀴즈

1. 클래스 내의 코드는 암묵적으로 strict mode가 적용된다. (O,X)

2. 클래스와 생성자 함수의 차이 3가지를 적어보시오.