예제 및 자세한 설명은 아래 mozilla 페이지 참고.
https://developer.mozilla.org/en/docs/Web/JavaScript/Closures
1. 개념
1) closure?
a function and the environment in which that function was created
2) when a function created?
javascript 의 thread 가 function을 선언한 부분을 execute할때.
** inner function 은 parent function 이 executed 될 때, created된다. parent function 이execute될 때마다 inner function은 created된다.
3) environment?
local variables + parent function's local variables + parameter + parent function's parameter
function은 closure 내 environment 에 접근 가능
2. closure 사용 예
1) private variable 설정
function 내 local variables 는 function이 executed된 후 사라진다. 그러나 function이 executed되면서 생성된 inner function들의 closure에는 살아있다.
function 외부에서 local variables에 대한 접근은 inner function을 통해서만 가능하다.
** local variables
the local variables within a function only exist for the duration of the function's execution
'Programming Practice > Javascript' 카테고리의 다른 글
prototype (0) | 2016.09.04 |
---|---|
함수 호출 (0) | 2016.08.23 |
delegation (0) | 2016.07.31 |
javascript convention (0) | 2016.07.31 |
Function (0) | 2016.07.30 |