第二篇ts,es6箭头函数结合typescript,和for...of
1.基本用法:
const f = v => v// 等同于const f = function(v) {return v}
2.箭头函数返回数组
const f = () => {const list = [1,2,3]// 直接返回一个对象return list.map(it => ({id: it}))}const result = f() // [{id:1},{id:2},{id:3}]
3.箭头函数和变量解构结合使用
const full = ({first,last}) => `${first}_${last}`// ? 实现可选参数的功能const full = (first:string,last?: string) => `${first}_${last}`const full = (first:string,last="smith") => `${first}_${last}`// 剩余参数const full = (first:string,...restOfName: string[]) => {return first + " " + restOfName.join(" ");}
}}
4.箭头函数结合默认值
const full = ({first,last}={first:'Mrs',last:'Right'})=> `${first}_${last}`
5.箭头函数结合typescript
const full = ( params: { first: string; last: string } )=> {const {first,last } =paramsreturn `${first}_${last}`}
for…of循环
const list = ['a','b','c']for(let val of list) {console.log('val', val)}const listObj = [{name:'a'},{name:'b'},{name:'c'}]for(let {name: item} of listObj) {console.log('name', item)}// a,b,c// 区分for...infor(let num in list) {console.log('num', num)}// 0,1,2
在单页运用的项目,箭头函数是最常用的,也是简洁的。