<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>se6 展开运算符</title>
</head>
<body>
<script>
/*
* spread operator 展开运算符
* 更快、更便捷的操作数据
*/
// 数组求和 reduce
function addNumbers1 (numbers) {
return numbers.reduce((sum,number) => {
return sum + number;
},0);
}
let numbers = [1,2,3,4,5];
console.log(addNumbers1(numbers));
// 数组求和 展开运算符 ...numbers形参为传递的参数数组
function addNumbers2 (...numbers) {
return numbers.reduce((sum,number) => {
return sum + number;
},0);
}
console.log(addNumbers2(1,2,3,4,5));
// 数组合并 concat
let defaultColors = ["red","green"];
let favoriteColors = ["oriange","yellow"];
let fallClors = ["fireRed","fallOriange"];
// defaultColors = defaultColors.concat(favoriteColors);
// console.log(defaultColors);
// 数组合并 展开运算符
defaultColors = ["blue","green",...defaultColors,...favoriteColors,...fallClors];
console.log(defaultColors);
// ("apple","eggs","orange") -> (...items) = items = ["apple","eggs","orange"]
function validateShoppingList(...items) {
if(items.indexOf('milk') < 0){
return ["milk",...items];
}
return items;
}
console.log(validateShoppingList("apple","eggs","orange"));
</script>
</body>
</html>
se6 展开运算符 ...
最新推荐文章于 2022-08-10 18:10:53 发布