2014-06-05 2 views
0

아래 코드를 실행하십시오 ... setTimeout 익명 함수에서 alert.hide() 메서드를 참조하는 올바른 방법은 무엇입니까? 전체 호출을 admin.alert.hide();으로 작성하는 것이 맞습니까? 또는 admin을 직접 호출 할 필요없이 참조 할 수있는 더 좋은 방법이 있습니까?중첩 IIFE 모듈에서 부모 JavaScript 함수를 참조하는 방법은 무엇입니까?

var admin = (function(jQuery, window, document, undefined) { 
    return { 
     loader : (function(admin) { 
      var fade = 75; 
      var loader = '#loader'; 
      return { 
       show : function() { 
        jQuery(loader).stop().fadeIn(fade); 
       }, 
       hide : function() { 
        jQuery(loader).stop().fadeOut(fade); 
       } 
      } 
     })(), 
     alert : (function() { 
      var timeout; 
      var fade = 500; 
      var milliseconds = 1000; 
      var alert = '#alert'; 
      return { 
       timeout : timeout, 
       show : function(message) { 
        jQuery(alert).find('p').text(message); 
        jQuery(alert).stop().fadeIn(fade); 
        clearTimeout(this.timeout); 
        this.timeout = setTimeout(function() { }, milliseconds); 
       }, 
       hide : function() { 
        jQuery(alert).stop().fadeOut(fade); 
       } 
      } 
     })() 
    } 
})(jQuery, window, document); 

답변

3

당신은 할 수 다음 ... "바인드"프로토 타입 방법

작동
return 
{ 
    timeout : timeout, 
    show : function(message) 
    { 
     jQuery(alert).find('p').text(message); 
     jQuery(alert).stop().fadeIn(fade); 
     clearTimeout(this.timeout); 
     this.timeout = setTimeout((function() { this.hide(); }).bind(this), milliseconds); 
    }, 
    hide : function() 
    { 
     jQuery(alert).stop().fadeOut(fade); 
    } 
} 
+0

입니까? –

+0

@AndrewB 예, 함수 클래스의 프로토 타입 함수입니다. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind –

관련 문제