Closure

Shortdary·2020년 7월 16일
0

사전적 의미

클로져 - In programming languages, a closure, also lexical closure or function closure, is a technique for implementing lexically scoped name binding in a language with first-class functions. Operationally, a closure is a record storing a function together with an environment. The environment is a mapping associating each free variable of the function (variables that are used locally, but defined in an enclosing scope) with the value or reference to which the name was bound when the closure was created. Unlike a plain function, a closure allows the function to access those captured variables through the closure's copies of their values or references, even when the function is invoked outside their scope.

일급함수(first-class function) - In computer science, a programming language is said to have first-class functions if it treats functions as first-class citizens. This means the language supports passing functions as arguments to other functions, returning them as the values from other functions, and assigning them to variables or storing them in data structures. Some programming language theorists require support for anonymous functions (function literals) as well. In languages with first-class functions, the names of functions do not have any special status; they are treated like ordinary variables with a function type. The term was coined by Christopher Strachey in the context of "functions as first-class citizens" in the mid-1960s.
보통 함수에 매개변수로 넘기기, 수정하기, 변수에 대입하기와 같은 연산을 지원할 때 일급 객체라고 한다.

어휘적 범위 지정(lexical scoping) - With lexical scope, a name always refers to its (more or less) local lexical environment. This is a property of the program text and is made independent of the runtime call stack by the language implementation. Because this matching only requires analysis of the static program text, this type of scoping is also called static scoping. Lexical scoping is standard in all ALGOL-based languages such as Pascal, Modula-2 and Ada as well as in modern functional languages such as ML and Haskell. It is also used in the C language and its syntactic and semantic relatives, although with different kinds of limitations. Static scoping allows the programmer to reason about object references such as parameters, variables, constants, types, functions, etc. as simple name substitutions. This makes it much easier to make modular code and reason about it, since the local naming structure can be understood in isolation. In contrast, dynamic scope forces the programmer to anticipate all possible dynamic contexts in which the module's code may be invoked.

그래서 클로저가 뭔데 십덕아 ㅋㅋ

클로저는 독립적인 (자유) 변수를 가리키는 함수이다. 또는, 클로저 안에 정의된 함수는 만들어진 환경을 ‘기억한다’.

클로져는 외부 변수 (lexical environment)에 reference를 가지는 함수이다.

보통 변수를 선언하면 그 블록이나 함수 안에서만 존재하는 거라고 생각을 한다. 블록이나 함수의 실행이 끝나면 그 변수의 수명은 끝이난다.

클로저는 코드 수행이 끝나서 블록 밖으로 나간 변수에 대한 참조를 하는 지속적인 범위이다.

예시)

outer = function() {
  var a = 1;
  var inner = function() {
    console.log(a);
  }
  return inner; // this returns a function
}

var fnc = outer(); // execute outer to get inner 
fnc();

함수가 끝나면 그것이 가진 변수는 모두 날아간다. 그런데 우리가 inner() 함수를 반환하여 fnc에 outer()를 할당하면 outer()는 끝이나있다. inner()의 범위 안의 변수들은 모두 지속된다.

profile
흐엥

0개의 댓글