hehongwei44 / my-blog

我的博客
MIT License
137 stars 12 forks source link

ES6结构、拓展运算符和rest运算入门 #132

Open hehongwei44 opened 8 years ago

hehongwei44 commented 8 years ago

解构

解构的作用是可以快速取得数组或对象当中的元素或属性,而无需使用arr[x]或者obj[key]等传统方式进行赋值

1.数组解构赋值:

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

2.嵌套数组解构:

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

3.函数传参解构:

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

4.for循环解构:

var arr = [[11, 12], [21, 22], [31, 32]];
for (let [a, b] of arr) {
  console.log(a);
  console.log(b);
}
//11
//12
//21
//22
//31
//32

5.对象赋值解构:

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 }

6.对象传参解构:

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

7.变量名与对象属性名不一致解构:

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,
  son: {
    sonname: '大熊',
    sonsex: 'male',
    sonage: 1
  }
};
var {name, sex, age, son: {sonname, sonsex, sonage}} = obj;
console.log(sonname + ' ' + sonsex + ' ' + sonage);
//大熊 male 1
//Babel暂不支持这种嵌套解构
obj = {
  name: 'chris',
  sex: 'male',
  age: [1, 2, 3]
}
{name, sex, age: [a, b, c]} = obj;
console.log(c);

8.嵌套对象属性重名,解构时需要更改变量名:

var obj = {
  name: 'chris',
  sex: 'male',
  age: 26,
  son: {
    name: '大熊',
    sex: 'male',
    age: 1
  }
};
//赋值解构
var {name: fathername, son: {name, sex, age}} = obj;
console.log(fathername); //chris
console.log(name); //大熊
//传参解构
function fn3({sex, age, name, son: {name: sonname}}) {
  console.log(name + ' ' + sex + ' ' + age + ' ' + sonname);
}
fn3(obj);
//chris male 26 大熊

9.循环解构对象:

var arr = [{name: 'chris', age: 26}, {name: 'jack',    age: 27}, {name: 'peter',age: 28}];

for (let {age, name} of arr) {
    console.log(name + ' ' + age);
}
//chris 26
//jack 27
//peter 28

10.解构的特殊应用场景:

//变量互换
var x = 1,
  y = 2;
var [x, y] = [y, x];
console.log(x); //2
console.log(y); //1
//字符串解构
var str = 'love';
var [a, b, c, d] = str;
console.log(a);//l
console.log(b);//o
console.log(c);//v
console.log(d);//e

扩展运算符

扩展运算符用三个点号表示,功能是把数组或类数组对象展开成一系列用逗号隔开的值

var foo = function(a, b, c) {
  console.log(a);
  console.log(b);
  console.log(c);
}
var arr = [1, 2, 3];
//传统写法
foo(arr[0], arr[1], arr[2]);

//使用扩展运算符
foo(...arr);
//1
//2
//3

特殊应用场景:

//数组深拷贝
var arr2 = arr;
var arr3 = [...arr];
console.log(arr===arr2); //true, 说明arr和arr2指向同一个数组
console.log(arr===arr3); //false, 说明arr3和arr指向不同数组

//把一个数组插入另一个数组字面量
var arr4 = [...arr, 4, 5, 6];
console.log(arr4);//[1, 2, 3, 4, 5, 6]

//字符串转数组
var str = 'love';
var arr5 = [...str];
console.log(arr5);//[ 'l', 'o', 'v', 'e' ]

rest运算符

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]

小结:

等号表达式是典型的赋值形式,函数传参和for循环的变量都是特殊形式的赋值。解构的原理是赋值的两边具有相同的解构,就可以正确取出数组或对象里面的元素或属性值,省略了使用下标逐个赋值的麻烦。

对于三个点号,三点放在形参或者等号左边为rest运算符; 放在实参或者等号右边为spread运算符,或者说,放在被赋值一方为rest运算符,放在赋值一方为扩展运算符。

hehongwei44 commented 8 years ago

原文链接:http://www.cnblogs.com/chrischjh/p/4848934.html?utm_source=tuicool&utm_medium=referral

hehongwei44 commented 7 years ago

参考地址: rest运算符:http://es6.ruanyifeng.com/#docs/function#rest参数 拓展运算符:http://es6.ruanyifeng.com/#docs/function#扩展运算符