简体   繁体   English

在另一个事件侦听器函数内定义的事件侦听器函数是否访问全局变量的副本?

[英]Does an event listener function defined inside another event listener function access the copy of global variables?

I was trying to make a div which could be moved to any position like drag and drop function. 我试图制作一个可以移动到任何位置(例如拖放功能)的div。 I used the following approach: 我使用以下方法:

 var songs = {}; songs.clickedM = 0; $(".song-progress .current-position")[0].addEventListener("mousedown", function(down2) { songs.clickedM = 1; var intpos2 = down2.clientX; $(".song-progress .current-position")[0].addEventListener( "mousemove", function(Dmove2) { if(songs.clickedM == 1) { if (Dmove2.clientX <= $(".song-progress").offset().left) { $(".song-progress .current-position")[0].style.left = "0px"; } else if( Dmove2.clientX >= ($(".song-progress").outerWidth() + $(".song-progress").offset().left)) { $(".song-progress .current-position")[0].style.left = ( $(".song-progress").outerWidth() - 14) + "px"; } else { $(".song-progress .current-position")[0].style.left = (Dmove2.clientX - $(".song-progress").offset().left ) + "px"; } } }); }); $("body")[0].addEventListener("mouseup", function() { songs.clickedM = 0; }); 
 .container { padding: 100px; width: 700px; height: 500px; background-color: lightgray; } .song-progress { position: absolute; top: 84px; right: 15px; height: 5px; width: calc(100% - 135px); background-color: white; } .current-progress{ position: absolute; left: 0; width: 0px; height: 5px; background-color: #bbb; } .current-time { position: absolute; bottom: 5px; font-size: 10px; left: 0; font-family: "Times New Roman" } .total-time { position: absolute; bottom: 5px; right: 0; font-size: 10px; font-family: "Times New Roman" } .current-position { height: 9px; width: 15px; background-color: #00cdff; position: absolute; top: -2.5px; left: 1px; cursor: pointer; border-radius: 5px; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="container"> <div class="song-progress"> <div class="current-time">1:21</div> <div class="total-time">5:37</div> <div class="current-progress"></div> <div class="current-position"></div> </div> </div> 

I noticed that songs.clickedM never becomes 0 , even when mouse key is released. 我注意到,即使释放鼠标键, songs.clickedM也永远不会变为0 My guess is that the mousemove event listener is a function which is acting like a function closure. 我的猜测是mousemove事件侦听器是一个函数,其作用类似于函数闭包。 And when the mouse moves for the first time after first click it accesses a copy of songs.clickedM not the original. 第一次单击后第一次移动鼠标时,它会访问songs.clickedM的副本, songs.clickedM不是原始副本。 It is unaware of the fact that the original variable songs.clickedM has actually been changed to 0 . 没有意识到原始变量songs.clickedM实际上已更改为0的事实。

How do I make the value of songs.clickedM 0 for mousemove event listener when the key is not pressed? 当未按下键时,如何为mousemove事件侦听器设置songs.clickedM 0的值?

My guess is that the mousemove event listener is a function which is acting like a function closure. 我的猜测是mousemove事件侦听器是一个函数,其作用类似于函数闭包。

It's not acting like a closure. 这不是像个封闭。 It is a closure. 一个关闭。 :-) :-)

And when the mouse moves for the first time after first click it accesses a copy of songs.clickedM not the original. 第一次单击后第一次移动鼠标时,它会访问songs.clickedM的副本, songs.clickedM不是原始副本。 It is unaware of the fact that the original variable songs.clickedM has actually been changed to 0 . 没有意识到原始变量songs.clickedM实际上已更改为0的事实。

No. Closures have access to the variables they close over, not a copy of the variables they close over. 不可以。闭包可以访问其关闭的变量,而不是其关闭的变量的副本 Even if they received a copy, the variable being closed over in this case is songs , which is just a reference to an object; 即使他们收到了副本,在这种情况下被关闭的变量songs ,这只是对对象的引用; a copy of it would still refer to the same object, with the same clickedM property. 它的副本仍将引用具有相同clickedM属性的同一对象。 All of your even handlers are using the same songs variable and the same clickedM property on the object it refers to. 您所有的偶数处理程序都在所引用的对象上使用相同的 songs变量和相同的clickedM属性。 The problem lies elsewhere. 问题出在其他地方。

Where that is will require debugging, but one red flag that shows up is that the code is adding a new mousemove handler every time the mouse is pressed on the element, even if it's been added previously. 需要进行调试的地方,但是出现的一个红色标记是,即使每次以前在元素上按下鼠标,代码也会每次都添加一个新的mousemove处理程序。 So those mousemove handlers start stacking up. 因此,这些mousemove处理程序开始堆叠。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM