The usage and meaning of the extended operator (...) in ES6

Extended operators in ES6

Meaning: To speak more popularly is to put the dataclothesTake it off. Remember this first. Look at the code behind to understand.

1. Display the elements in the array and the values ​​in the object
var list=[1,2,3,4,5]
console.log(...list)
//输出 1,2,3,4,5
//这里就是相当于直接扒掉数组的外壳
var person ={name:'huihui',age :23}
console.log({...person})
//输出:{name:'huihui',age :23}
2. Copy numbers or objects
var list =[1,2,3]
var list2 = [4,5,6]
//这里直接把list和list2的外壳扒了,放到list3中形成一个合并数组
var list3 = [...list,...list2]
//这里再把数组的外壳扒掉
console.log(...list3)
//输出:1 2 3 4 5 6
3. Replace the original attributes with the newly added object attributes
var person = {name:'灰太狼',age:23}
var person = {...person,age:80}//用在对象中会把相同的属性用后者覆盖前者
console.log({...person})
//这里用新的age可以覆盖掉原来的age
4. Use in function parameters
function add(x,y){
	console.log(x+y)
}
var num = [1,2]
add(...num)
//输出:3

Summary: The above is a common way to expand operators, does it feel easy to understand! Just remember to just strip the shell of the data. Welcome to discuss if you have any questions.

Guess you like

Origin blog.csdn.net/qq_44606064/article/details/106413672