let x = 1;
let y = 2;
console.log('x:'+x, 'y:'+y);
[x,y] = [y,x];
console.log('x:'+x, 'y:'+y);
- 2.用途二: 从函数返回多个值
//函数只能返回一个值,如果要返回多个值,要将他们放在数组或者对象里返回。
function example(){
return [1,2,3]
}
let [a,b,c]= example();
console.log('a:'+a,'b:'+b,'c:'+c);
function example2(){
return {
name:'kang+',
age:20
}
}
let {name,age} = example2();
console.log('name:'+name,'age:'+age);
function f([x,y,z]){
console.log('x:'+x);
console.log('y:'+y);
console.log('z:'+z);
}
f([1,2,3]);
function f({x,y,z}){
console.log('x:'+x);
console.log('y:'+y);
console.log('z:'+z);
}
f({z:3, x:2, y:1});
- 4.用途四:提取json数据(项目开发中一定用的到的)
let jsonData = {
number:01,
status:'200',
data:[{person:'kang+',age:20}]
}
let {number,status,data,data:[{person,age}]} = jsonData;
console.log('number:'+number);
console.log('status:'+status);
console.log(data)
console.log('person:'+person);
console.log('age:'+age);
- 5.用途五:函数参数的默认值 (这个个人认为作用不大)
function Person(name,age,school = 'xiyou'){
this.name = name;
this.age = age;
this.school = school;
}
var son = new Person('kang+',20);
console.log(son)
Map 结构原生支持 Iterator接口,配合变量的解构赋值获取键名和键值就非常方便。
var map = new Map();
map.set('name','kang+');
map.set('age','20');
for(let [key, value] of map){
console.log(key + ' is ' + value);
}
name is kang+
age is 20
获取键名
var map = new Map();
map.set('name','kang+');
map.set('age','20');
for(let [key] of map){
console.log(key);
}
var map = new Map();
map.set('name','kang+');
map.set('age','20');
for(let [,value] of map){
console.log(value);
}