箭头函数使用

一:定义方式

let add=(a,b)=>{

return a+b;
}
console.log(add(3,4));
如果参数只有一个函数()可以省略:
 
let show=a=>{return a};
console.log(show(1));
箭头函数this不会绑定全局 代码如下:
console.log(show(1));
function counter(){
this.num=0;
this.timer=setInterval(function(){this.num++;console.log(this.num)})
}
let b=new counter();//underfined ...
/* 箭头函数使用*/
console.log(show(1));
function counter(){
this.num=0;
this.timer=setInterval(()=>{this.num++;console.log(this.num)})
}
let b=new counter();//1,2,3,4....
 

猜你喜欢

转载自www.cnblogs.com/xys-/p/10545683.html