흥미로운 범위 체인의 사례는 많은 문서에서 설명하지 못했지만 이해하기 어렵습니다. 누군가 아래에 잘 주석 된 코드를 읽고 변수가 해결되는 방법을 설명하는 데 시간이 걸릴 수 있다면 매우 좋을 것입니다.마우스 처리기 내의 Javascript 실행 컨텍스트, 카운터 직관적 인 범위 체인
문서에 두 개의 직사각형 (DIV)이 있습니다. 등록 된 mousedown 리스너 내에서 mousedown에 대한 이벤트 리스너를 등록합니다. 이상한 일이 mouseup의 청취자 내에서 일어나고 있습니다.
두 실행 컨텍스트가 인수 다른 값 회 testfunc 호출에 의해 생성되어 상기 제 사각형의 mouseup에 listener 내
window.onload = function() {
test_func("horizontal"); // First Execution context
test_func("vertical"); // Second Execution Context
}
(가로)이 제 2 실행 환경 (세로)의 대향되는 사용되고 직관적 인 :
function test_func(dir) {
var X = 9; // variable which helps to track the execution contexts
if(dir === "horizontal")
X = 1; // leave at 9 if vertical
mouseup_vert = function() {}
mouseup_horiz = function() {
// Here the value of X I am getting is 9 whereas I am expecting 11
// QUESTION: Why I am getting the second execution context??
}
mousedown_vert = function() {
// As expected the value of X here is 9
X=99;
// set X to 99 to check if during mouseup same exec context is picked
document.addEventListener("mouseup", mouseup_vert, false);
}
mousedown_horiz = function() {
// As expected value of X is 1, so using first execution context
X=11;
// set this to check if during mouseup I get a value of 11
document.addEventListener("mouseup", mouseup_horiz, false);
}
if (dir === "horizontal") {
e = document.getElementById("horiz");
e.addEventListener("mousedown", mousedown_horiz, false);
} else {
e = document.getElementById("vert");
e.addEventListener("mousedown", mousedown_vert, false);
}
}
. "mouseup_foo"변수는 모두 전역 변수입니다. 그들은'var' 키워드로 선언되어야합니다 !! 또한 이벤트 핸들러 내부에서 이벤트 핸들러를 설정하는 것이 큰 문제가 될 것입니다. – Pointy
@Pointy : 핸들러 할당에 대한 좋은 지적. – user113716
세계적인 경우 X는 mousedown_horiz에서 11로 설정됩니다.이 값은 mouseup_horiz에서 볼 수있는 값이며, 이는 내가보고있는 것이 아니며 따라서 내 질문이 아닙니다. – Raks