三个点
以前我们复制数组可能是这样的
str1 = [10,5,6,2]
str2 = Array.from(str1)
现在我们可以用三个点(拓展运算符)
str1 = [10,5,2,9]
//str2 = Array.from(str1)
str2 = [...str1]
console.log(str2)
同样我们得到:
那么到底这三个点是什么呢?
它起一个拓展、解构的作用,…arr返回的就是数组的元素
他甚至可以拓展字符串
第二个强大的地方是
接收参数
function show() {
console.log(arguments)
}
show(1,5,6,3)
更好的方法是什么呢?
function show(...args) {
console.log(args)
}
show(1,5,6,3)
是不是很方便,取到的参数自动转成一个数组
如果需要排序的话,我们只需要使用数组的sort方法就可以啦
剩余参数
function show(str,...args) {
console.log(str,args)
}
show("happy",1,5,6,3)
是不是很强大,大家要多加运用。。。。