2012-05-29 6 views
0

외부 함수에 변수가 있고 해당 변수를 변경하는 내부 함수가있는 경우 해당 변수가 원래 값으로 되돌아가는 이유는 무엇입니까?외부 함수의 변수를 수정할 수 없습니다.

// Check which type of event registration browser supports 
function addEvent(eventTarget, eventType, eventHandler){ 
    if (window.addEventListener){ 
     eventTarget.addEventListener(eventType, eventHandler); 
    } 
    else if (window.attachEvent){ 
     var eventType = "on" + eventType; 
     eventTarget.attachEvent(eventType, eventHandler); 
    } 
} 

// Check if browser supports DOMContentLoaded 
var DOMContentLoadedSupported = "no"; 

addEvent(document, "DOMContentLoaded", function(){ 
    DOMContentLoadedSupported = "yes"; 
}) 

alert(DOMContentLoadedSupported) // alerts "no" not "yes". Why??    
​ 
+2

DOMContentLoaded 이벤트가 발생할 때까지 "no"로 경고합니다. – jbabey

답변

2

기능은 변수를 수정합니다,하지만 당신은 변수가 수정되기 전에 경고가 발생 경고 원인에 "아니오"를 참조하십시오 그래서, 당신의 경고 후에 발생합니다.

+0

'DOMContentLoaded'의 요점은 경고와 같은 다른 것보다 먼저 발생하지 않는가? 나는 왜 경고가 먼저 일어날 지 이해하지 못하고있다. – user1019031

+1

@ user1019031 'DOMContentLoaded'의 포인트는 DOM 콘텐츠가로드 될 때 발생하지 않습니다. 문서에서'var DOMContentLoadedSupported = "addEventListener"와 동 기적으로 지원을 확인할 수 있습니다. – Esailija

관련 문제