擴展運算符就是三個點「...」,就是將實現了Iterator 接口的對象中的每一個元素都一個個的迭代並取出來變成單獨的被使用。;擴展運算符用三個點號表示,功能是把數組或類數組對象展開成一系列用逗號隔開的值數組
arr1.push(...arr2) // 把arr2合併到arr1的後面 arr1.unshift(...arr2) //把arr2合併到arr1的前面
等同於:app
arr1.concat(arr2) arr2.concat(arr1)
若是你想在數組內合併數組,你能夠像下面這樣作:函數
var arr1 = ['two', 'three']; var arr2 = ['one', ...arr1, 'four', 'five'];
等同於下面代碼:this
var arr1 = ['two', 'three']; var arr2 = ['one'].concat(arr1, ['four', 'five']);
const arr = [1,2,3]; let arr2 = [...arr]; // 就像 arr.slice() arr2.push(4); console.log(arr); console.log(arr2); 等同於: var arr = [1, 2, 3]; var arr2 = [].concat(arr); // 就像 arr.slice() arr2.push(4); console.log(arr); console.log(arr2); const arr3 = [4,5,6] let arr4 = arr3 arr4.push(7); console.log(arr3); console.log(arr4);
結果:rest
[1, 2, 3] [1, 2, 3, 4] [4, 5, 6, 7] [4, 5, 6, 7]
注意:對象的解構與數組有一個重要的不一樣。數組的元素是按次序排列的,變量的取值由它的位置決定;而對象的屬性沒有次序,變量必須與屬性同名,才能取到正確的值。code
let { x, y, ...z } = { x: 1, y: 2, a: 3, b: 4 }; console.log(x); // 1 console.log(y); // 2 console.log(z); // { a: 3, b: 4 }
var arr = ['this is a string', 2, 3]; //傳統方式 var a = arr[0], b = arr[1], c = arr[2]; //解構賦值,是否是簡潔不少? var [a, b, c] = arr; console.log(a);//this is a string console.log(b);//2 console.log(c);//3
注意:對象
數組的元素是按次序排列的,變量的取值由它的位置決定;接口
var arr = [[1, 2, [3, 4]], 5, 6]; var [[d, e, [f, g]], h, i] = arr; console.log(d);//1 console.log(f);//3 console.log(i);//6
var arr = ['this is a string', 2, 3]; function fn1([a, b, c]) { console.log(a); console.log(b); console.log(c); } fn1(arr); //this is a string //2 //3
var arr = [[11, 12], [21, 22], [31, 32]]; var arra=[]; var arrb=[] for (let [a, b] of arr) { arra.push(a) arrb.push(b) } console.log(arra); console.log(arrb);
結果:three
[11, 21, 31] [12, 22, 32]
var obj = { name: 'chris', sex: 'male', age: 26, son: { sonname: '大熊', sonsex: 'male', sonage: 1 } }; var {name, sex, age, son} = obj; console.log(name + ' ' + sex + ' ' + age); //chris male 26 console.log(son); // { sonname: '大熊', sonsex: 'male', sonage: 1 }
注意:string
對象的屬性沒有次序,變量必須與屬性同名,才能取到正確的值。
var obj = { name: 'chris', sex: 'male', age: 26, son: { sonname: '大熊', sonsex: 'male', sonage: 1 } }; function fn2({sex, age, name}) { console.log(name + ' ' + sex + ' ' + age); } fn2(obj); //chris male 26
var obj = { name: 'chris', sex: 'male', age: 26 }; var {name: nickname, age: howold} = obj; console.log(nickname + ' ' + howold); //chris 26
等同於:
var obj = { name: 'chris', sex: 'male', age: 26 }; var nickname = obj.name, howold = obj.age; console.log(nickname + ' ' + howold); //chris 26
var obj = { name: 'chris', sex: 'male', age: 26, son: { sonname: '大熊', sonsex: 'male', sonage: 1 } }; var {name, sex, age, son: {sonname, sonsex, sonage}} = obj; console.log(sonname + ' ' + sonsex + ' ' + sonage);
等同於:
var obj = { name: 'chris', sex: 'male', age: 26, son: { sonname: '大熊', sonsex: 'male', sonage: 1 } }; var name = obj.name, sex = obj.sex, age = obj.age, _obj$son = obj.son, sonname = _obj$son.sonname, sonsex = _obj$son.sonsex, sonage = _obj$son.sonage; console.log(sonname + ' ' + sonsex + ' ' + sonage);
rest運算符也是三個點號,不過其功能與擴展運算符剛好相反,把逗號隔開的值序列組合成一個數組
//主要用於不定參數,因此ES6開始能夠再也不使用arguments對象 var bar = function(...args) { for (let el of args) { console.log(el); } } bar(1, 2, 3, 4); //1 //2 //3 //4 bar = function(a, ...args) { console.log(a); console.log(args); } bar(1, 2, 3, 4); //1 //[ 2, 3, 4 ]
rest運算符配合解構使用:
var [a, ...rest] = [1, 2, 3, 4]; console.log(a);//1 console.log(rest);//[2, 3, 4]