ES6(...)展开运算符

函数传参

展开运算符可以展开数组,所以不再需要apply方法,将数组转为函数的参数了

                 // ES5 的写法
                 function f(x, y, z) {
                 // ...
                 }
                 var args = [0, 1, 2];
                 f.apply(null, args);
                 // ES6 的写法
                 function f(x, y, z) {
                 // ...
                 }
                 var args = [0, 1, 2];
                 f(...args);

数据解构

其实就是把数组的每个数据拆开然后放进去

                let arr = ['autumn', 'wscats'];
		// 析构数组
		let y;
		[autumn, ...y] = arr;
		console.log(y) // ["wscats"]

数据构造

两个对象连接返回新的对象

               let x = {
			name: 'autumn'
		}
		let y = {
			age: 18
		}
		let z = {...x,...y}
		console.log(z)

两个数组连接返回新的数组

                let x = ['autumn']
		let y = ['wscats']
		let z = [...x, ...y]
		console.log(z)// ["autumn", "wscats"]

数组加上对象返回新的数组

                let x = [{
			name: 'autumn'
		}]
		let y = {
			name: 'wscats'
		}
		let z = [...x, y];
		console.log(z);

ES6(...)展开运算符_第1张图片

数组+字符串

                let x = ['autumn'];
		let y = 'wscats';
		let z = [...x, y];
		console.log(z);

数组+对象

               let x = {
			name: ['autumn','wscats'],
			age:18
		}
		let y = {
			...x,//name: ['autumn','wscats'],age:18
			arr: [...x.name]//['autumn','wscats']
			
		}
		console.log(y)

ES6(...)展开运算符_第2张图片 

你可能感兴趣的:(javascript)