카테고리 없음

화살표 함수의 this 바인딩.

Hyunsoo_Kim 2023. 9. 14. 01:02
    const obj={
        checkThis(){
            function inner(){
                console.log(this);
            }
            inner();
        }
    }
    
    // Window {window: Window, self: Window, document: document, name: '', location: Location, …}

    const objTwo={
        checkThis(){
            (()=>{console.log(this);})();
        }
    }

    obj.checkThis();

    objTwo.checkThis();
    
    // {checkThis: ƒ}

화살표 함수의 this는 상위스코프의 this를 참조한다.

 

일반 함수의 this는 전역객체를 참조한다.