دنبال کننده ها

۱۳۹۶ مهر ۱۱, سه‌شنبه

() => vs function() in JavaScript

[ad_1]

Can someone tell me what the difference is between the () =>
and function() is?



I tried to do some array.map() and it got messed up with the shorter version of function.



menuItems.map(item => item.addEventListener('click', function (e) 
console.log(this);
));


And for some reason when using () => instead of the function() , this had a value of



Window → http://localhost:3000/


When using the normal anonymous function() , this had a value of



<div class="email-item pure-g" data-id="1">


Which is what I wanted.



So why is it acting differently?



[ad_2]

لینک منبع