使用箭头函数更加简洁了函数
setTimeOut(function()){},1000)
====> setTimeOut(()=>{},1000)
省略了return关键字
let arr=[1,3,5]
arr.filter(item=>item===3) [3]
下面看一个箭头函数去掉数组里的undefined返回新的数组:
const compact = arr => arr.filter(res=>res!=undefined)
compact([0, 1, false, 2, '', 3, 'a', undefined, 's', 34])
const compact=arr=>arr.filter(res=>res!=undefined)其实是下面的:
function compact(arr){
arr.filter(res=>{
if(res!=undefined){return res}
})
}
for ... of是作为ES6新增的遍历方式,允许遍历一个含有iterator接口的数据结构并且返回各项的值,和ES3中的for ... in的区别如下
for ... of遍历获取的是对象的键值,值,for ... in 获取的是对象的键名,下标
for ... in会遍历对象的整个原型链,性能非常差不推荐使用,而for ... of只遍历当前对象不会遍历原型链
let arr=['1','3','9'];
for(x of arr){console.log(x)} 1,3,9 返回的是键对应的值
for(x in arr){console.log(x)} 0,1,2 返回的是下标,也就是键。
let arr=[{a:1},{a:2},{a:3}];
for(x of arr){console.log(x)} {a: 1}, {a: 2}, {a: 3} 返回的是键对应的值。等同于for (x in arrs){console.log(arrs[x])}
for(x in arr){console.log(x)} 0,1,2 返回的是下标,也就是键。