A closure is the combination of a function bundled together (enclosed) with references to its surrounding state (the lexical environment). In other words, a closure gives you access to an outer function’s scope from an inner function. In JavaScript, closures are created every time a function is created, at function creation time. - MDN

MDN에서 Closure에 대한 원문 설명은 위와 같습니다.

클로저는 함수와 함수가 선언된 어휘적 환경의 조합이다. 클로저를 이해하려면 자바스크립트가 어떻게 변수의 유효범위를 지정하는지(Lexical Scoping)를 먼저 이해해야 한다. - MDN

MDN 한국어 번역본에서 Closure에 대한 설명은 위와 같습니다.

function outerFn() {
  let outerVar = 'outer';
  console.log(outerVar);
  function innerFn() {
  	let innerVar = 'inner';
    console.log(innerVar);
  }
  return innerFn;
}
let globalVar = 'global';
let innerFn = outerFn();
innerFn();

클로저는 외부 함수의 변수에 접근할 수 있는 내부 함수, 또는 이러한 작동 원리를 일컫는 용어로 클로저 함수 안에서는 지역변수(innerVar), 외부 함수의 변수(outerVar), 전역 변수(globalVar)에 대한 접근이 전부 가능합니다.

코드 및 자료 출처: 코드스테이츠(CodeStates)

0개의 댓글