简体   繁体   English

在每次调用JavaScript时都重新定义嵌套函数的变量?

[英]Redefining nested function's variable from each call to parent in JavaScript?

I've got a nested function inside another JavaScript function; 我在另一个JavaScript函数中有一个嵌套函数; the nested function accepts a variable that is passed to the parent. 嵌套函数接受一个传递给父级的变量。 The first time I call the parent, all variables are set correctly. 第一次调用父级时,所有变量均已正确设置。 However any subsequent times that I call the parent, the nested function doesn't apply the new variable that was passed to the parent (instead it persists the variable from the first call). 但是,在我随后调用父级的任何后续时间中,嵌套函数都不会应用传递给父级的新变量(相反,它将保留第一次调用中的变量)。

For example: 例如:

myFunc: function(path, points) {
   var onDrag      = function(e) {

       // This is the variable I want redefined each time I 
       // call myFunc with new points
       console.log('points: ', points);

    },
    onDragStart   = function(e) {
      window.addEventListener('mousemove', onDrag);
    };

    this.el.addEventListener('mousedown', onDragStart);

    return this;
}

The above is a stripped-down version of my method. 上面是我的方法的简化版本。 I call myFunc when I create my element, and as you can see onDrag gets called on mousedown on the element. 创建元素时,我调用myFunc ,并且您可以看到onDrag在元素上的mousedown上被调用。 But, I also call myFunc directly at several points later in the script, passing it new variables for path and points, expecting onDrag 's references to these variables to update. 但是,我还将在脚本的后面几个点直接调用myFunc ,将路径和点的新变量传递给myFunc ,期望onDrag对这些变量的引用会更新。

However, this isn't happening, instead onDrag is persisting the points variable from the first time I called it. 但是,这并没有发生,而是onDrag从我第一次调用它时就一直保留points变量。

How can I have my nested function update it's variables each time I call its parent? 每次调用父函数时,如何使嵌套函数更新其变量?

What I've tried: 我尝试过的

I've tried passing points as an argument to the onDrag , like this: 我尝试将点作为onDrag的参数onDrag ,如下所示:

onDrag = function(e, points) { }

I've tried redefining a variable inside the onDrag scope, like this: 我尝试过在onDrag范围内重新定义变量,如下所示:

onDrag = function(e) { var pts = points; }

I've tried not defining the function in a variable, like this: 我试过不在变量中定义函数,如下所示:

function onDrag(e) { }

But none of these things has worked. 但是这些事情都没有奏效。

If you call the "myFunc" again it will again attach add new eventListner on window object. 如果再次调用“ myFunc”,它将再次在窗口对象上附加添加新的eventListner。 You need to first remove previously attached event from window. 您需要先从窗口中删除以前附加的事件。

window.removeEventListener('mousemove', onDrag, false); window.removeEventListener('mousemove',onDrag,false);

you need to pass the arguments to onDrag at the time that you invoke it. 您需要在调用onDrag时将其传递给它。

myFunc: function(path, points) {
   var onDrag      =  function(e, points) {

        // This is the variable I want redefined each time I 
        // call myFunc with new points
        console.log('points: ', points);


    },

    onDragStart   = function(e, points) {
      window.addEventListener('mousemove', onDrag(e, points));
    };

    this.el.addEventListener('mousedown', onDragStart(e, points));

    return this;
}

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

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