ES6箭头函数

ES6越来越火,不会箭头函数就out了,今天就来讲一下箭头函数得使用!!

//普通写法
function func (text) {
    console.log(text);
}
func('普通函数');

//箭头函数写法
let func2 = (text)=>{
     console.log(text);
}
func2('箭头函数');

//一个参数箭头函数省略括号写法
let func3 = text=>{
    console.log(text);
}
func3('一个参数箭头函数省略括号写法');

//return语句只有一句得时候return和大括号{}都能去掉
let arr = [1,2,3,4,5];
let func4 = arr.map(function(item,index){
    return item > 2?'普通函数':'箭头函数'
})
    

let func4 = arr.map(item =>{
    return item > 2?'普通函数':'箭头函数'
});

let func4 = arr.map(item => item > 2?'普通函数':'箭头函数')
console.log(func4)        

  你们学会了吗?

posted @ 2019-03-19 17:59  一八嘚酒  阅读(117)  评论(0编辑  收藏  举报