3.7.1 다양한 값 대신 화살표 함수 사용 해결 방법

    과거 습관: 콜백에서 호출 컨텍스트 this에 접근할 수 있는 다양한 해결 방법은 다음과 같다.

    변수 사용, 즉 var self = this;

    Function.prototype.bind 사용

    지원한다면 함수의 thisArg 매개변수 사용

    예를 들어 보면 다음 코드와 같다.

    // 변수 사용
    var self = this;
    this.entries.forEach(function(entry) {
        if (entry.matches(str)) {
            self.appendEntry(entry);
        }
    });
    
    // Function.prototype.bind 사용
    this.entries.forEach(function(entry) {
        if (entry.matches(str)) {
            this.appendEntry(entry);
        }
    }.bind(this));
    
    // 'thisArg' 사용
    this.entries.forEach(function(entry) {
        if (entry.matches(str)) {
            this.appendEntry(entry);
        }
    }, this);

    새로운 습관: 화살표 함수 사용

    this.entries.forEach(entry => {
        if (entry.matches(str)) {
            this.appendEntry(entry);
        }
    });
    신간 소식 구독하기
    뉴스레터에 가입하시고 이메일로 신간 소식을 받아 보세요.