箭头函数(Arrow Function)
目录:
语法
单一参数的单行箭头函数
用法:arg => statement
const fn = foo => `${foo} hello`; // means retuen `foo + 'hello'`
常用于简单的处理函数,比如过滤:
let array = ['a', 'bc', 'def', 'ghij'];
array = array.filter(item => item.length >= 2); // bc, def, ghij
多参数的单行箭头函数
用法:(arg1, arg2) => statement
const fn = (foo, bar) => foo + bar
常用于数组的处理,如排序:
let array = ['a', 'bc', 'def', 'ghij'];
array = array.sort((a, b) => a.length < b.length);