箭头函数如何使用

function add(a,b){

return a+b

}

箭头函数的写法:

(a,b)=>{

return a+b

}

如用箭头函数进行数组的升序和降序

let arr = [10, 20, 1, 2];

arr.sort((x,y)=>{

return (y-x)

})

Console.log(arr);//[20,10,2,1]

arr.sort((x, y) => {

 return(x-y)

});

console.log(arr); // [1, 2, 10, 20]

使用箭头函数注意:

  • 没有this、super、arguments、newtarget绑定,箭头函数中的this、super、arrgument以及new.target这些值由外围最近一层非箭头函数决定。
  • 不能通过new关键字调用 箭头函数没有[[Construct]]方法,所以不能被用作构造函数
  • 没有原型  箭头函数不存在prototype这个属性
  • 不可以改变this绑定  函数内部this值不可以改变,箭头函数中的this值取决于该函数外部非箭头函数的值,并且不能通过bind(),apply(),call()方法改变this的值
  • 不支持arguments对象 箭头函数没有arguments绑定,所以必须通过命名参数和不定参数这两种形式访问函数的参数。
  • 不支持重复的命名参数

 

 

posted @ 2018-12-27 16:45  realman?  阅读(1134)  评论(0编辑  收藏  举报