2014-04-24 1 views
0

following code을 이해하려고합니다. 정확히 말하면 연구 방법은 $ .proxy()입니다. 나는이 스크립트를 가지고 때까지 모든 것이 분명했다 :이 상황에서 jQuery.proxy()는 어떻게 작동합니까?

(function($, exports){ 
    var mod = function(){}; 
    mod.fn = mod.prototype; 

    mod.fn.proxy = function(func){ 
     return $.proxy(func, this); 
    }; 
    mod.fn.load = function(func){ 
     $(this.proxy(func)); 
    }; 
    exports.Controller = mod; 
})(jQuery, window); 

(function($, Controller){ 
    var mod = new Controller; 
    mod.toggleClass = function(e){ 
     this.view.toggleClass("over", e.data); 
    }; 
    mod.load(function(){ 
     this.view = $("#view"); 
     this.view.mouseover(this.proxy(this.toggleClass)); 
     this.view.mouseout(this.proxy(this.toggleClass)); 
    }); 
})(jQuery, Controller); 

를 그리고 나는 $ .proxy()이 부분에서 어떻게 작동하는지 이해가 안 :

mod.fn.load = function(func){ 
    $(this.proxy(func)); // Why it is converted into an object jQuery? 
}; 

는 사람이 어떻게 작동하는지 설명 할 수 있습니까?

답변

0

$(this.proxy(func));은 jQuery 객체가 아니므로 $(this.proxy(func));은 DOM 준비가 완료되기 전에 load 메서드가 호출 되더라도 dom 메소드가 준비 될 때까지 지연되도록 콜백 함수가 지연되도록 DOM 메소드에 전달 된 함수를 DOM 준비 핸들러로 등록합니다. DOM이 이미 준비 상태이면 콜백이 지연없이 실행됩니다.

mod.fn.load = function (func) { 
    console.log('inside load') 
    $(this.proxy(func)); 
    console.log('exit load') 
}; 

데모 : 그것은 동일 Fiddle

mod.fn.load = function (func) { 
    var self = this; 
    //dom ready callback 
    $(function(){ 
     func.call(self); 
    }); 
}; 

등의 데모 : 당신이 일을하지 않는 경우 Fiddle


그럼 스크립트가 가능성이 있습니다

0과 같이 실패합니다.
mod.fn.load = function (func) { 
    var self = this; 
    console.log('inside load'); 
    func.call(self); 
    console.log('exit load') 
}; 

데모 : Fiddle

+0

감사합니다. 이제 나는 이해했다.)) –