<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>展开语法</title>
</head>
<body>
<script>
//合并数组
let hxj =["hao","xue","jie"];
let ydc=["yang","ding","chuan"];
console.log(hxj);// ["hao", "xue", "jie"]
console.log(...hxj);//hao xue jie
//使用for-in循环ydc追加到hxj
/* for(let key in ydc){
hxj.push(ydc[key]);
}
console.log(hxj);//["hao", "xue", "jie", "yang", "ding", "chuan"]
*/
//注意数组循环for-of 里的循环变量直接就是值
/* for(const value of ydc){
hxj.push(value);
}
console.log(hxj);//["hao", "xue", "jie", "yang", "ding", "chuan"]
*/
//使用展开语法合并数组
let arr=[...hxj,...ydc];
console.log(arr);
//使用展开语法传递多个参数
function sum(...args){
return args.reduce((s,i)=>{
return (s += i);
},0)
}
console.log(sum(1,2,3));//6
</script>
</body>
</html>
网友评论