sitelink1 | |
---|---|
sitelink2 | |
sitelink3 | |
sitelink4 | http://1 |
extra_vars4 | ko |
extra_vars5 | http://blog.morrisjohns.com/javascript_closures_for_dummies |
extra_vars6 | sitelink1 |
JavaScript Closures for Dummies
begin content- a closure is the local variables for a function - kept alive after the function has returned, or
- a closure is a stack-frame which is not deallocated when the function returns. (as if a 'stack-frame' were malloc'ed instead of being on the stack!)
클로져는 펑션이 리턴된 후 계속해서 펑션의 상태를 유지시키는 로컬 변수이다.
클로져는 펑션이 리턴될 때 해제되지 않는 stack-frame 이다.
The following code returns a reference to a function:
function sayHello2(name) {
var text = 'Hello ' + name; // local variable
var sayAlert = function() { alert(text); }
return sayAlert;
}
There is a critical difference between a C pointer to a function, and a JavaScript reference to a function. In JavaScript, you can think of a function reference variable as having both a pointer to a function as well as a hidden pointer to a closure.
C와 JavaScript 의 펑션 사이에는 중요한 차이점이 있다. 자바스크립트의 경우에는, 펑션에 대한 포인터 뿐만아니라 클로져에 대한 숨겨진 포인터 둘다를 가지는 펑션 레퍼런스 변수를 생각할 수 있다.
The above code has a closure because the anonymous function function() { alert(text); } is declared inside another function, sayHello2() in this example. In JavaScript, if you use the function keyword inside another function, you are creating a closure.
코드상에는 클로져가 있다. 예제에서 익명function (function() { alert(text); })는 또다른 function(sayHello2()) 내에 선언되어 있기 때문이다. 자바스크립에서는 만약 당신이 어떠한 function내에서 또다시 "function" 키워드를 사용하게 된다면 클로져를 생성하고 있다는 것을 의미한다.
In C, and most other common languages after a function returns, all the local variables are no longer accessable because the stack-frame is destroyed.
C를 포함하여 대부분의 다른 일반적인 랭귀지들은 펑션 리턴후에 모든 로컬 변수들은 더이상 접근할 수 없다. 왜냐하면 stack-frame 이 제거되기 때문이다.
In JavaScript, if you declare a function within another function, then the local variables can remain accessable after returning from the function you called. This is demonstrated above, because we call the function say2(); after we have returned from sayHello2(). Notice that the code that we call references the variable text, which was a local variable of the function sayHello2().
자바스크립트의 경우에는 만약 당신이 어떤 function을 또다른 function내에 선언한다면 로컬 변수들은 당신이 호출하여 function 리턴된 후에도 접근가능 상태를 유지할 수 있다. 이것은 그 이상을 의미하기도 한다. 왜냐하면 sayHello2() function을 리턴한 후에 우리는 say2(); function을 호출하기 때문이다. 코드는 우리가 function sayHello2()의 로컬 변수였던 variable text의 레퍼런스를 호출한다는 것을 알려준다.
function() {
alert(text);
}
Click the button above to get JavaScript to print out the code for the anonymous function. You can see that the code refers to the variable text. The anonymous function can reference text which holds the value 'Jane' because the local variables of sayHello2() are kept in a closure.
The magic is that in JavaScript a function reference also has a secret reference to the closure it was created in - similar to how delegates are a method pointer plus a secret reference to an object.
댓글 0
번호 | 제목 | 글쓴이 | 날짜 | 조회 수 |
---|---|---|---|---|
117 | User Agent 관련 Reference URL | 황제낙엽 | 2011.02.22 | 41 |
116 | 각 브라우저 별 User Agent 정보 | 황제낙엽 | 2011.02.22 | 823 |
115 | History of User Agent | 황제낙엽 | 2011.02.22 | 38 |
114 | Navigator 객체란? | 황제낙엽 | 2011.02.22 | 53 |
113 | Understanding User-Agent Strings | 황제낙엽 | 2011.02.22 | 76 |
112 | User Agent 정보 모음 | 황제낙엽 | 2011.02.22 | 7768 |
111 | ActiveX 설치 여부를 검사하는 스크립트 | 황제낙엽 | 2011.02.13 | 4053 |
110 | 자바스크립트 예약어 | 황제낙엽 | 2010.11.03 | 35 |
109 | YUI Logger(Yahoo) 를 동적으로 로드하는 북마크릿 | 황제낙엽 | 2010.10.03 | 25 |
108 | Javascript 를 사용하여 Binary File 읽기 | 황제낙엽 | 2010.09.29 | 500 |
107 | 크로스 브라우저를 위한 브라우저 검사 코드 | 황제낙엽 | 2010.08.27 | 86 |
106 | Dynatrace For Ajax Performance | 황제낙엽 | 2010.08.18 | 45 |
105 | javascirpt IME-Mode 설정하기 | 황제낙엽 | 2010.08.17 | 1112 |
104 | Iframe 내의 페이지 접근방법 | 황제낙엽 | 2009.11.12 | 59 |
103 | 외부 라이브러리 (.js) 의 바람직한 동적 로딩 (The best way to load external JavaScript) | 황제낙엽 | 2009.10.05 | 124 |
102 | 숫자값으로의 변환 형태 | 황제낙엽 | 2009.09.02 | 18 |
101 | Boolean 데이터 타입 | 황제낙엽 | 2009.09.02 | 16 |
100 | toString 변환 테이블 | 황제낙엽 | 2009.09.02 | 13 |
99 | URI 인코딩을 해야 하는 문자들 | 황제낙엽 | 2009.09.02 | 23 |
98 | 체인 생성자(생성자 체인), 프로토타입 체인 그리고 생성자 재지정 | 황제낙엽 | 2009.08.12 | 55 |