I have a class where a function binds mouse events to SVG element:
class Chart {
bindEvent() {
this.svg.append('rect')
.on('mousemove', () => {
const mouseDate = scales.x.invert(d3.mouse(this)[0]);
});
}
}
Which then babel transpiles into:
var Chart = function () {
function Chart() {
_classCallCheck(this, Chart);
}
_createClass(Chart, [{
key: 'bindEvent',
value: function bindEvent() {
var _this = this;
this.svg.append('rect').on('mousemove', function () {
var mouseDate = scales.x.invert(d3.mouse(_this)[0]);
});
}
}]);
return Chart;
}();
So within the event callback 'this' changes '_this' and I loose context. Is there a way to manage this issue?