1.箭头函数与function定义函数的写法:

//function
function fn(a, b){
    return a + b;
}
//arrow function
var foo = (a, b)=>{ return a + b };

2.this的指向:使用function定义的函数,this的指向随着调用环境的变化而变化,而箭头函数中的this指向是固定不变的,一直指向定义函数的环境。

//使用function定义的函数
function foo(){
    console.log(this);
}
var obj = { aa: foo };
foo(); //Window
obj.aa() //obj { aa: foo }

 

//使用箭头函数定义函数
var foo = () => { console.log(this) };
var obj = { aa:foo };
foo(); //Window
obj.aa(); //Window

 

3.变量提升

由于js的内存机制,function的级别最高,而用箭头函数定义函数的时候,需要var(let const定义的时候更不必说)关键词,而var所定义的变量不能得到变量提升,故箭头函数一定要定义于调用之前

 

本文转载:https://blog.csdn.net/github_38851471/article/details/79446722

 

相关文章:

  • 2021-11-14
  • 2022-02-10
  • 2021-05-23
  • 2021-05-25
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-05-25
  • 2022-12-23
  • 2021-08-29
相关资源
相似解决方案