2013-03-03 6 views
0

javascript noob로서 함수에서 변수를 다른 함수로 가져올 수있는 방법을 이해하는 데 어려움이 있습니다.함수에서 중첩 함수로 변수를 보낼 수있는 방법

xx.projects.resize에 설정된 winHeight을 xx.projects.item.next에 사용하고 싶습니다. 거기에 console.log (winHeight)가 표시됩니다.

여기서 무엇을 살펴야합니까? 이 this.winWidththis.winHeight를 사용

var xx = { 

    init: function() 
    { 
     xx.listener.init() 
    }, 


    listener: { 

     init: function() 
     { 
      xx.listener.resize() 
     }, 

     resize: function() 
     { 
      xx.projects.resize() 
      $(window).on('resize',function(){ 
       xx.projects.resize() 
      }) 
     }, 
    }, 

    projects: { 

     resize: function() 
     { 
      var $display = $('section .item.curr'); 
      var $displayNotActive = $('section.curr .item.next, section.curr .item.prev'); 

      var winWidth = $(window).width(); 
      var winHeight = $(window).height(); 

      var containerWidth, containerHeight; 

      if(winWidth > 1450) { 
       if(winHeight > 1050) { 
        containerWidth = 1200; 
        containerHeight = 900; 
       } else { 
        containerWidth = winWidth - 250; 
        var helperHeight = Math.floor(containerWidth * 3/4); 
        if(helperHeight > (winHeight - 150)) { 
         containerHeight = winHeight - 150; 
         containerWidth = Math.floor(containerHeight * 4/3); 
        } else { 
         containerHeight = helperHeight; 
        } 
       } 
      } else if(winWidth < 600) { 
       containerWidth = 300; 
       containerHeight = 225; 
      } else { 
       containerWidth = winWidth - 250; 
       var helperHeight = Math.floor(containerWidth * 3/4); 
       if(helperHeight > (winHeight - 150)) { 
        containerHeight = winHeight - 150; 
        containerWidth = Math.floor(containerHeight * 4/3); 
       } else { 
        containerHeight = helperHeight; 
       } 
      } 
     }, 


     item: { 
      next: function() 
      { 
       var s = $('#projects section.curr') 
        a = s.find('.item.curr'), 
        n = a.next('.item'), 
        l = a.position() 

       console.log(winHeight); 

       if(n.length > 0){ 
        a.animate({ left: '-100%' }, ep.projects.config.item.speed, ep.projects.config.item.easing) 
        n.animate({ left: l.left }, ep.projects.config.item.speed, ep.projects.config.item.easing, function(){ 
         a.removeClass('curr').addClass('prev') 
         n.removeClass('next').addClass('curr')      
        }) 
       }    
      }, 
     }, 
    } 
} 

$(document).on('ready', xx.init) // Document loaded, DOM ready 
+0

당신은 프로그래밍 또는 JS 멍청한 놈인가? 다른 언어로 어떻게 하시겠습니까? – Bergi

+0

@Bergi 거의 모두. – INT

답변

0

오브젝트 액세스 범위에 winHeightwinWidth 선언.

var xx = { 

    projects: { 
     resize: function() { 
      //we are saving winWidth, winHeight in the projects object scope so that it can be accessed from other functions in the same scope 
      this.winWidth = $(window).width(); 
      this.winHeight = $(window).height(); 

      var containerWidth, containerHeight; 

      if (this.winWidth > 1450) { 
       if (this.winHeight > 1050) { 
        containerWidth = 1200; 
        containerHeight = 900; 
       } else { 
        containerWidth = this.winWidth - 250; 
        var helperHeight = Math.floor(containerWidth * 3/4); 
        if (helperHeight > (this.winHeight - 150)) { 
         containerHeight = this.winHeight - 150; 
         containerWidth = Math.floor(containerHeight * 4/3); 
        } else { 
         containerHeight = helperHeight; 
        } 
       } 
      } else if (winWidth < 600) { 
       containerWidth = 300; 
       containerHeight = 225; 
      } else { 
       containerWidth = this.winWidth - 250; 
       var helperHeight = Math.floor(containerWidth * 3/4); 
       if (helperHeight > (this.winHeight - 150)) { 
        containerHeight = this.winHeight - 150; 
        containerWidth = Math.floor(containerHeight * 4/3); 
       } else { 
        containerHeight = helperHeight; 
       } 
      } 
     }, 


     item: { 
      next: function() { 
       var s = $('#projects section.curr') 
       a = s.find('.item.curr'), 
        n = a.next('.item'), 
        l = a.position() 

        console.log(this.winHeight); 

       if (n.length > 0) { 
        a.animate({ 
         left: '-100%' 
        }, ep.projects.config.item.speed, ep.projects.config.item.easing) 
        n.animate({ 
         left: l.left 
        }, ep.projects.config.item.speed, ep.projects.config.item.easing, function() { 
         a.removeClass('curr').addClass('prev') 
         n.removeClass('next').addClass('curr') 
        }) 
       } 
      }, 
     }, 
    } 
} 

http://jsfiddle.net/FDeKe/

+0

'resize'가 어떻게 호출되는지 아십니까? 그것은 이벤트 핸들러가 될 수 있고 잘못된 ['this' 값] (https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Operators/this)은 OP를 또 다른 비트로 혼동 할 것입니다. – Bergi

+0

나는 이해하지 못한다. 이벤트 핸들러에서 호출되면 어떻게 될까요? 그게 어떻게 영향을 미칠까? – deadlock

+0

시도해 보라 :'$ (window) .resize (xx.project.resize)'(그리고 내가 전에 링크 한 페이지를 읽는다.) – Bergi

관련 문제