交换变量的值
let a = 1;
let b = 2;
[a, b] = [b, a];
console.log(a, b); //2 1
函数返回多个值
//返回数组时
function example1() {
return [1, 2, 3]
}
let [a, b, c] = example1();
console.log(a, b, c); // 1 2 3
//返回对象时
function example2() {
return {
one: 1,
two: 2
}
}
let { one, two } = example2()
console.log(one, two); // 1 2
函数参数的定义
//参数是一组有序的值
function fun1([a, b, c]) {
console.log(a, b, c)
}
fun1([1, 2, 3]); // 1 2 3
//参数是一组无序的值
function fun2({ x, y, z }) {
console.log(x, y, z)
}
fun2({ z: 3, x: 2, y: 1 }); //2 1 3
提取JSON数据
let JsonData = {
id: 10,
status: "OK",
data: [111, 222]
}
let { id, status, data: numbers } = JsonData;
console.log(id, status, numbers); //10 "OK" [111, 222]
遍历
const map = new Map();
map.set('first', 1);
map.set('second', 2);
for (let [key, value] of map) {
console.log(key + ":" + value);
//first:1
//second:2
}
如果单独获取的话,可以写成下面这样
//获取键名
for (let [key] of map) {
}
//获取键值
for (let [, value] of map) {
}
输入模块的指定方法
const {firstThing,secondThing} = requier("path")