ES6学习笔记-Spread Operator 展开运算符

ES6中 还有一个特别好玩的特性就是Spread Operator也就是三个点**…**(这里不是省略号)

下面介绍一下他能干啥。

组装对象或者数组

//数组
const color = [ 'red' , 'yellow' ]
const colorful = [...color, 'green' , 'pink' ]
console.log(colorful)

//对象
const alp = { first:'a',second:'b' }
const alphabets = { ...alp,third: 'c'}

有的时候我们想获取数组或者对象 除了前几项或者除了某几项的其他项

//数组
const number = [1,2,3,4,5]
const [first,...rest] = number
console.log(rest)

//对象
const user = {
	username:'will',
	gender:'female',
	age:19,
	address:‘peking’
}
const { username,...rest } = user
console.log(rest) //{"address": "peking", "age": 19, "gender": "female"

对于Object而言,还可以用于组合成新的Object

const first ={
	a:1,
	b:2,
	c:6
}
const second = {
	c: 3,
	d: 4
}
const total = {...first,...second}

你可能感兴趣的:(ES6)