📜  为什么要使用箭头函数而不是普通函数 - Javascript 代码示例

📅  最后修改于: 2022-03-11 15:01:57.955000             🧑  作者: Mango

代码示例1
// => why to choose arrow function over normal function 
1. short syntax, good as callback functions
 ([].map(() => {}) is better than [].map(function () {}))

2. when using class component, with arrow functions no need to bind this

// example: 

 -// this will be undefined
        button.addEventListener('click', () => {console.log(this); })
  // this will refer to dom button
        button.addEventListener('click', function () {console.log(this); })