简体   繁体   English

JavaScript范围:在事件处理程序中访问变量?

[英]JavaScript scope: access variable inside event handler?

I have the following code. 我有以下代码。 It draws a stacked graph in D3, and then allows the user to filter the lines that are shown, by selecting an option from a drop-down list. 它在D3中绘制堆叠图形,然后允许用户通过从下拉列表中选择一个选项来过滤显示的行。

However, I'm having problems with the scope. 但是,我的范围有问题。 When I use the dropdown, the console statement shows that series 1 has updated correctly. 当我使用下拉列表时,控制台语句显示series 1已正确更新。 But series 2 , inside the mousemove code, has not updated. 但是,mousemove代码中的series 2尚未更新。

I guess that's because it's bound and has not updated when the variable updates. 我猜这是因为它已经绑定,并且在变量更新时没有更新。 How can I get access to the updated variable? 如何访问更新的变量?

 d3.csv("/data/test.csv", function(error, data) {

  function updateGraph(label) {

    // Filter the data. 
    var series = data.filter(function(d, i) {
      return d.Type == label;
    });
    console.log('series 1', series); // This updates correctly. 

    // Draw the paths. 
    var items = svg.selectAll("path").data(newSeries);
    items.enter().append('path').attr("d", function(d) {
      return area(d.data);
    }).on("mousemove", function(d, i) {
      console.log('series 2', series); // This is out of date. 
    });
  }

  // Draw the initial graph. 
  updateGraph('initial');
  // When the user changes the drop-down, redraw the graph. 
  d3.select("#chooseoption").on("change", function(val) {
    updateGraph(this.options[this.selectedIndex].value);
  });

});

If you want there to be a single series variable that multiple event handlers can access or modify, then you need to define that variable at a scope above all the event handlers that want to participate so they all share access to the same variable rather than multiple copies of the variable exist in multiple closures. 如果您希望有一个多个事件处理程序可以访问或修改的单个series变量,那么您需要在所有想要参与的事件处理程序的范围内定义该变量,以便它们共享对同一变量的访问权限而不是多个变量的副本存在于多个闭包中。

In this case, you could do it like this: 在这种情况下,您可以这样做:

d3.csv("/data/test.csv", function(error, data) {

  // define series variable where multiple event handlers can share it
  var series;
  function updateGraph(label) {

    // Filter the data. 
    series = data.filter(function(d, i) {
      return d.Type == label;
    });
    console.log('series 1', series); // This updates correctly. 

    // Draw the paths. 
    var items = svg.selectAll("path").data(newSeries);
    items.enter().append('path').attr("d", function(d) {
      return area(d.data);
    }).on("mousemove", function(d, i) {
      console.log('series 2', series); // This is out of date. 
    });
  }

  // Draw the initial graph. 
  updateGraph('initial');
  // When the user changes the drop-down, redraw the graph. 
  d3.select("#chooseoption").on("change", function(val) {
    updateGraph(this.options[this.selectedIndex].value);
  });

});

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

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