ES6 展开运算符 三个点实际功能
1.数组中使用
let defaultColors = ['red', 'greed'] let favoriteColors = ['orange', 'yellow'] let fallColors = ['pink', 'blue'] ES5 defaultColors.concat(favoriteColors) ES6 [...defaultColors , ...favoriteColors ] ES5 defaultColors.concat(favoriteColors) defaultColors.concat(fallColors) ES6 [...defaultColors , ...favoriteColors, ...fallColors ] ['purple', ...defaultColors , ...favoriteColors, ...fallColors, 'black' ] //可在数组中直接放入,还可新增其他字符串
2.函数中使用
ES5 function addNumber(a, b, c, d, e){ let numbers = [a, b, c, d, e] retrun numbers.reduce((sum, number) => { retrun sum + number },0) } console.log(addNumber(1,2,3,4,5)) ES6 function addNumber(...numbers){ retrun numbers.reduce((sum, number) => { retrun sum + number },0) } console.log(addNumber(1,2,3,4,5))
建议将代码放入编辑器中查看,会更明显