📎아이템 49 콜백에서 this에 대한 타입 제공하기
class C {
vals = [1, 2, 3];
logSquares() {
for (const val of this.vals) {
console.log(val ** 2);
}
}
}
const c = new C();
c.logSquares();
// 코드를 실행하면
1
4
9const c = new C();
const method = c.logSquares;
method();
// 런타임 오류 발생
Uncought TypeError: undefined의 'vals' 속성을 읽을 수 없다.🔗 this 바인딩 제어
📍 정리
📍 요약
Last updated