javascript - 使用 ES6 箭头函数时,D3.js 事件监听器无法访问 "this"

标签 javascript d3.js ecmascript-6

<分区>

我正在尝试使用带有箭头函数的 D3.js 事件监听器,但它似乎不起作用。

this 绑定(bind)到 undefined

如何使用 ES6 箭头函数访问 this

ES5:

svg.selectAll('circle')
  .data(data)
  .enter()
  .append('circle')
  .attr('r', '10')
  .attr('cx', (d) => x(d.day))
  .attr('cy', (d) => y(d.amount))
  .on('mouseenter', function (d) {
    console.log(this); // output '<circle r="10" cx="10" cy="1"/>'
  });

ES6(使用箭头函数):

svg.selectAll('circle')
  .data(data)
  .enter()
  .append('circle')
  .attr('r', '10')
  .attr('cx', (d) => x(d.day))
  .attr('cy', (d) => y(d.amount))
  .on('mouseenter', (d) => {
    console.log(this); // output: 'undefined'
  });

最佳答案

这是预期的行为。 (以下是我试图解释这种行为的糟糕尝试。你可能会过得更好 reading this )

执行箭头函数的上下文 (this) 将是定义它们的上下文(无论 this 在函数之外)

D3 可能会将事件监听器的上下文设置为发出事件的对象(如 ES5 示例中所示)。

但是,通过使用箭头函数,您强制将上下文绑定(bind)到您定义函数的上下文。在您的情况下,此上下文未定义/窗口,因为您的代码未包含在另一个函数中。

如果我将您的 ES6 示例转换回 ES5 可能会更好地解释):

var self = this;    
svg.selectAll('circle')
      .data(data)
      .enter()
      .append('circle')
      .attr('r', '10')
      .attr('cx', (d) => x(d.day))
      .attr('cy', (d) => y(d.amount))
      .on('mouseenter', (d) => {
        console.log(self); // output: 'undefined'
      });

我对解决您的问题的建议很简单。使用常规 function 作为您的事件订阅者(对您的两个“attr”订阅者使用箭头函数也没有任何好处)。

svg.selectAll('circle')
  .data(data)
  .enter()
  .append('circle')
  .attr('r', '10')
  .attr('cx', (d) => x(d.day))
  .attr('cy', (d) => y(d.amount))
  .on('mouseenter', function(d) {
    console.log(this); // output '<circle r="10" cx="10" cy="1"/>'

  });

关于javascript - 使用 ES6 箭头函数时,D3.js 事件监听器无法访问 "this",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34086193/

相关文章:

javascript - jquery ddslick 不工作

javascript - HTML5 移动应用程序在手机屏幕关闭时运行?

javascript - KoGrid:默认排序不起作用

javascript - 加载asp.net网格后调用Jquery函数

javascript - 这种奇怪的搜索模式(解构)在 MongoDB promises 中是如何工作的?

javascript - 凹坑 x2 轴需要位置绑定(bind)到 x

d3.js - d3js 强制网络 : Ctrl-Click node to external link?

d3.js - 如何在 brush "brush"事件处理程序中获取选定数据?

javascript - 从数组创建对象

javascript - 将 ES6 类对象序列化为 JSON