3.11 bind 분석하기

    이 절에서는 함수의 메서드인 bind를 분석해보겠습니다. bind 메서드에는 수많은 오버로딩이 있습니다. 이 장의 코드는 타입스크립트 5.0.4 버전의 코드입니다. 5.1 버전에서는 bind의 코드가 다릅니다.

    우선 다음과 같이 간단한 코드를 작성합시다.

    function a(this: Window | Document) {
      return this;
    }
    const b = a.bind(document);
    const c = b();
    

    bind 메서드에서 마우스 오른쪽 버튼을 클릭해 Go to References 메뉴에 들어가면 다음과 같은 타입을 확인할 수 있습니다.

    lib.es5.d.ts

    interface Function {
      (...)
      bind(this: Function, thisArg: any, ...argArray: any[]): any;
      (...)
    }
    
    신간 소식 구독하기
    뉴스레터에 가입하시고 이메일로 신간 소식을 받아 보세요.