-2

I have a study code that I am currently using:

<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <title></title>
    <script src="https://d3js.org/d3.v5.min.js"></script>
  </head>
  <body>

    <script type="text/javascript">
      var svg = d3.select('body').append('svg').attr('width', 500).attr('height', 500);

      var data = [30, 20, 40];

      var circles = svg.selectAll('circle')
        .data(data).enter()
        .append('circle')
          .attr('cx', (d, i) => i * 30 + 20)
          .attr('cy', 30)
          .attr('r', d => d / 2)
          .style('fill', 'steelblue');

      circles.on('mouseenter', () => {
        d3.select(this).style('fill', 'red');
      });
      circles.on('mouseout', function() {
        d3.select(this).style('fill', 'steelblue');
      })
    </script>
  </body>
</html>

The mouseenter function is not working, but the mouseout function is fine.
Please tell me the difference about function() {} with () => {}

Trooper Z
  • 1,617
  • 14
  • 31
yz_n
  • 25
  • 3

2 Answers2

0

The difference is the lexical use of this in (fat) arrow functions.

Jake explains it perfectly here- What is lexical 'this'?

Joe
  • 74
  • 1
  • 10
0

One difference is that function(){} gets it's own this where ()=>{} gets the this from where it was called. This article explains more in depth the differences between the two.

mika
  • 343
  • 1
  • 14