2014-09-30 6 views
-2

나는 isRetina 함수를 가지고 있으며 다른 resizeImage() 내에서 결과에 액세스하려고하지만 변수 retinaCheck는 항상 나를 다시 정의하지 않습니다. 글로벌 변수를 처음부터 선언했는데, 왜 그런가?다른 함수 내에서 함수 결과에 액세스하는 중

$(window).load(function() { 
    function isRetina() { 
     return ((window.matchMedia && (window.matchMedia('only screen and (min-resolution: 192dpi), only screen and (min-resolution: 2dppx), only screen and (min-resolution: 75.6dpcm)').matches || window.matchMedia('only screen and (-webkit-min-device-pixel-ratio: 2), only screen and (-o-min-device-pixel-ratio: 2/1), only screen and (min--moz-device-pixel-ratio: 2), only screen and (min-device-pixel-ratio: 2)').matches)) || (window.devicePixelRatio && window.devicePixelRatio > 2)) && /(iPad|iPhone|iPod)/g.test(navigator.userAgent); 
    } 

    var retinaCheck = isRetina(); 

}); 

function resizeImage() { 
    if ($(window).width() > 649) { 
     console.log("resize:" + retinaCheck) 

    } 
}); 
+1

전체 코드를 표시합니다. – Cheery

+0

잘 작동합니다 : http://jsfiddle.net/rx0khzp0/1/ – DhruvPathak

+0

디버깅 도움말 ("이 코드가 작동하지 않는 이유는 무엇입니까?")에는 원하는 동작, 특정 문제 또는 오류 ** 및 가장 짧은 코드가 포함되어야합니다. 질문 자체에서 그것을 재현하는 데 필요한 **. 문제를 재현하는 코드가 없으면 (귀하의 코드는 그렇게하지 않습니다.) - 우리가 당신을 도울 수있는 방법은 거의 없습니다. –

답변

0

당신은 window.load 외부에서 전역 변수를 선언 할 수 window.load 내부 여기에 값을 할당합니다. resizeImage() 기능을 호출하면 retinaCheck 값을 읽을 수 있습니다.

var retinaCheck; 
$(window).load(function() { 
    function isRetina(){ 
     return ((window.matchMedia && (window.matchMedia('only screen and (min-resolution: 192dpi), only screen and (min-resolution: 2dppx), only screen and (min-resolution: 75.6dpcm)').matches || window.matchMedia('only screen and (-webkit-min-device-pixel-ratio: 2), only screen and (-o-min-device-pixel-ratio: 2/1), only screen and (min--moz-device-pixel-ratio: 2), only screen and (min-device-pixel-ratio: 2)').matches)) || (window.devicePixelRatio && window.devicePixelRatio > 2)) && /(iPad|iPhone|iPod)/g.test(navigator.userAgent); 
    } 

    retinaCheck = isRetina(); 

}); 

function resizeImage() { 
    if ($(window).width() > 649) {   
      console.log("resize:"+retinaCheck)    
     }); 
} 
+1

그는 이미 retinaCheck를 전역 변수로 정의했습니다. – DhruvPathak

+0

그 차이는 무엇입니까? 이 문제를 수정하는 시나리오가 있더라도 상황을 고려하지 않고이 답변을 제공하는 것은 그리 도움이되지 않습니다. –

+0

업데이트 대답 –

관련 문제