十.数组解构

const numbers = ['one', 'two', 'three', 'four']

  

es5:

const one = numbers[0];    //one     
const two = numbers[1];    //two

  

es6: 获取到相应位置的数组原素的值

const [one, two] = numbers;

console.log(one,two);  //one two

//如果要获取数组0跟3 位置的元素的话,把那个位置留出来就行
const [one, , tow] = numbers;

//如果你想获取第一个元素的值和后面所有元素的值的话 (...others必须是在最后的一个位置)
const [one,...others] = numbers;
console.log(one,others);  // one ["two","three","four"]

  

es6默认参数:

const details = ['JellyBool', 'wangrong.com', null];

const [name,website,category= 'PhP'] = details;

console.log(name, website, category);    // JellyBool wangrong.com null (只有category为undefined时,category值才为Php)

  

例子:交换 a 跟 b 的值

let a = 10;
let b = 20;

 

es5:

let temp;
temp = a;
a = b;
b = temp;
console.log(a,b);    //20 10

  

es6:

[a,b] = [b,a];

console.log(a,b);    //20 10

  

posted @ 2019-11-25 10:55  小蓉儿  阅读(146)  评论(0编辑  收藏  举报