javascript
초보자를위한 자바 스크립트 Closure
Closure 쉽게 생각 하면 아주 간단합니다.
함수내 함수를 선언 해서 사용하면 보통 클로저라고 한다.
function test(name){
var text = name+' 네임추가';
var tk = function() {
console.log(text);
}
tk();
}
test('테스트');
클러저 사용할때 메모리 누수 발생
https://www.codeproject.com/Articles/12231/Memory-Leakage-in-Internet-Explorer-revisited
jquery Closure
함수내 함수를 선언 해서 사용하면 보통 클로저라고 한다.
function test(name){
var text = name+' 네임추가';
var tk = function() {
console.log(text);
}
tk();
}
test('테스트');
- 함수내에 다른 함수 내부에서 사용할때 클로저가 사용됩니다.
- eval() 내부에서 함수를 사용할때 클로저 사용됩니다.
(eval 함수의 로컬변수참조) - 새 함수는 외부 함수의 로컬 변수를 참조 할수 없음
- 자바스크립트의 클로저는 마치 함수가 종료될때 처럼 모든 로컬 변수의 본사본을 유지하는것과 같음
- 클러저는 항상 함수에 대한 항목으로 만 생성됨 , 그리고 클로저는 추가 된다는 생각을 해야됨
- 클러저가 있는 함수가 호출 될때마다 새로운 지역변수 세트가 유지
클러저 사용할때 메모리 누수 발생
https://www.codeproject.com/Articles/12231/Memory-Leakage-in-Internet-Explorer-revisited
jquery Closure
(function($) { /** * Namespace: the namespace the plugin is located under * pluginName: the name of the plugin */ var extensionMethods = { /* * retrieve the id of the element * this is some context within the existing plugin */ showId: function(){ return this.element[0].id; } }; $.extend(true, $[ Namespace ][ pluginName ].prototype, extensionMethods); })(jQuery);
(function($) { var fullCalendarOrg = $.fn.fullCalendar; $.fn.fullCalendar = function(options) { if(typeof options === "object") { options = $.extend(true, options, { // locale isRTL: false, firstDay: 1, // some more options }); } var args = Array.prototype.slice.call(arguments,0); return fullCalendarOrg.apply(this, args); } })(jQuery);
댓글 쓰기
0 댓글