1、交换变量的值
let a= 1, b = 2;
[a, b] = [b, a];
2、获得多个返回值
function aa(){
return [1,2,3]
}
let [a, b, c] = aa() // a->1 b->2 c->3
function bb(){
return {name: 'bob', age: 11}
}
let {name, age} = bb()// name->'bob' age->11
3、函数参数的定义
function aa([x, y, z]){}
aa([1,2,3])
function bb({x, z, y}){}
bb({y:1, x: 2, z: 444})
4、提取JSON数据,如后台接口的数据解析
async getInfo(){
let {code, msg, data} = await getUser({userId: 2222})
}
5、封装函数时,设置默认值。如项目中封装的 post, get 请求的函数
export function(url, data, header = {'Content-Type': 'application/json'}){
.....
}
6、引入某个文件中的某些方法
import {getList, postMsg} from '@/views/api/user'
7、遍历Map解构
let map = new Map()
map.set('first', 111)
map.set('second', 222)
//输出key和value
for(let [key, value] of map){
console.log(key, value)
}
// 输出key
for(let [key] of map){
console.log(key)
}
// 输出value
for(let [ , value] of map){
console.log(value)
}
就记录到这,祝大家开心!