TypeScript Deep Dive
  • README
  • 시작하기
    • 왜 타입스크립트인가
  • 자바스크립트
    • 비교 연산자
    • 참조 연산자
    • Null vs. Undefined
    • this
    • 클로저
    • Number
    • Truthy
  • 미래의 자바스크립트
    • 클래스
      • 즉시실행함수
    • 화살표 함수
    • 나머지 연산자
    • let
    • const
    • 비구조화 할당
    • 전개 연산자
    • for...of
    • 이터레이터
    • 템플릿 리터럴
    • 프로미스
    • 제네레이터
    • Async Await
  • 프로젝트
    • 컴파일러 제어
      • tsconfig.json
      • 파일 경로 지정
    • 선언
    • 모듈화
      • 파일을 이용한 모듈화
      • globals.d.ts
    • 네임스페이스
    • 동적 표현식 가져오기
  • Node.js 시작하기
  • Browser 시작하기
  • 타입스크립트 타입 시스템
    • 자바스크립트 마이그레이션 가이드
    • @types
    • 주변 선언
      • 파일 선언
      • 변수
    • 인터페이스
    • 열거형(Enums)
    • lib.d.ts
    • 함수
    • 콜러블(Callable)
    • 타입 표명(Type Assertion)
    • 신선도(Freshness)
    • 타입 가드
    • 리터럴(Literal)
    • 읽기 전용(readonly)
    • 제네릭
    • 타입 인터페이스
    • 타입 호환성
    • Never 타입
    • 구별된 유니온
    • 인덱스 서명(Index Signature)
    • 타입 이동하기
    • 예외 처리
    • 믹스인(Mixin)
  • JSX
    • React
    • Non React JSX
  • Options
    • noImplicitAny
    • strictNullChecks
  • 타입스크립트 에러
    • 에러 메세지
    • 공통 에러
  • NPM
  • 테스트
    • Jest
    • Cypress
  • Tools
    • Prettier
    • Husky
    • ESLint
    • Changelog
  • 팁
    • 문자열 Enums
    • 타입 단언
    • 상태 저장 함수
    • 커링
    • 제네릭 타입 예시
    • 객체 타입 설정
    • 유용한 클래스
    • Import / Export
    • 속성 Setters
    • outFile 주의사항
    • 제이쿼리 팁
    • 정적 생성자
    • 싱글톤 패턴
    • 함수 파라미터
    • 토글 생성
    • Import 여러개 하기
    • 배열 생성
    • 생성자에서 타입정의
  • 스타일 가이드
  • 타입스크립트 컴파일러 구조
    • Program
    • AST
      • TIP: Visit Children
      • TIP: SyntaxKind enum
      • Trivia
    • Scanner
    • Parser
      • Parser Functions
    • Binder
      • Binder Functions
      • Binder Declarations
      • Binder Container
      • Binder SymbolTable
      • Binder Error Reporting
    • Checker
      • Checker Diagnostics
      • Checker Error Reporting
    • Emitter
      • Emitter Functions
      • Emitter SourceMaps
    • Contributing
Powered by GitBook
On this page

Was this helpful?

  1. 자바스크립트

클로저

자바스크립트에서 얻은 가장 좋은 점은 클로저였습니다. 클로저는 외부 변수에도 스코프 밖에서 접근할 수 있게 해줍니다. 클로저는 사용하는 가장 좋은 방법을 설명합니다.

function outerFunction(arg) {
    var variableInOuterFunction = arg

    function bar() {
        console.log(variableInOuterFunction) // Access a variable from the outer scope
    }

    // Call the local function to demonstrate that it has access to arg
    bar()
}

outerFunction('hello closure') // logs hello closure!

내부 함수가 외부 스코프의 변수에 접근할 수 있음을 예제를 통해 알 수 있습니다. 외부 함수의 변수는 내부 함수에 의해서만 접근이 가능합니다. 그러므로 클로저라는 용어로 사용되고 그 자체로 개념은 꽤 직관적입니다.

중요한 부분: 내부 함수는 외부 함수가 반환한 후에도 외부 스코프의 변수에 접근할 수 있습니다. 이것은 내부 함수가 외부 변수에 여전히 묶여있고 외부 함수에 의존적이지 않기 때문입니다. 다시 예제를 보겠습니다.

function outerFunction(arg) {
    var variableInOuterFunction = arg
    return function() {
        console.log(variableInOuterFunction)
    }
}

var innerFunction = outerFunction('hello closure!')

// Note the outerFunction has returned
innerFunction() // logs hello closure!

클로저가 엄청난 이유

그것은 객체를 쉽게 구성할 수 있도록 하고 모듈 패턴으로 구성됩니다.

function createCounter() {
    let val = 0
    return {
        increment() {
            val++
        },
        getVal() {
            return val
        }
    }
}

let counter = createCounter()
counter.increment()
console.log(counter.getVal()) // 1
counter.increment()
console.log(counter.getVal()) // 2

높은 수준에서 Node.js와 같은 것을 만들 수 있습니다.🌹

// Pseudo code to explain the concept
server.on(function handler(req, res) {
    loadData(req.id).then(function(data) {
        // the `res` has been closed over and is available
        res.send(data)
    })
})
PreviousthisNextNumber

Last updated 6 years ago

Was this helpful?