这是一个范围链的有趣案例,很多文档中都没有解释,我觉得很难理解。如果有人可以花些时间阅读下面的注释良好的代码并解释如何解决该变量,那将非常好
我在文档上有两个矩形(DIV)。我在mousedown侦听器中以及在mouseup侦听器中都注册了mousedown的事件侦听器。在mouseup的侦听器中发生了奇怪的事情。
通过两次调用具有不同参数值的testfunc来创建两个执行上下文:
window.onload = function() {
test_func("horizontal"); // First Execution context
test_func("vertical"); // Second Execution Context
}
在第一个矩形(水平)的mouseup侦听器内,正在使用第二个执行上下文(垂直),这非常直观:
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);
}
}
最佳答案
这是因为引用函数的变量未使用var
声明,因此它们是全局的。
它们被第二个调用覆盖,第二个调用以X
的值在9
附近关闭。
编辑:这是实际上正在发生的事情。为简单起见,我将所有变量都放在同一范围内。
// "a" is referencing a function.
var a = function() { alert( 'a' ); };
// "b" is referencing the same function that "a" is referencing.
var b = a;
// The reference "a" holds to the function is being overwritten with a
// reference to a new function.
a = function() { alert( "I'm a new function" ); };
// "c" is referencing the same function that "a" is referencing (the new one).
var c = a;
// So what is "b" referencing? It is still the original function, because it
// picked up that reference *before* the original reference "a" held was
// overwritten.
b(); // alerts 'a'
...或者使代码更具体地针对您的代码:
// reference a function
var mouseup_vert = function() { alert( "I'm a mouseup function." ); };
// reference a function
var mousedown_vert = function() {
// When this function is invoked, add a listener using
// whatever function is referenced by mouseup_vert
document.addEventListener( "mouseup", mouseup_vert, false);
};
// The function currently referenced by "mousedown_vert" is passed here
// as the event handler.
document.addEventListener("mousedown", mousedown_vert, false);
// Overwrite "mouseup_vert" just for kicks.
mouseup_vert = function() { alert( "I'm a completely different function." ); };
// NOW trigger a "mousedown" event. What happens? The function passed as the
// handler is invoked.
// What does that function do? It adds a "mouseup" handler.
// Which variable is referenced? "mouseup_vert".
// What is the current value of "mouseup_vert"? The new function, since we
// overwrote the reference to the original.