<!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>扩展运算符在函数中的运用</title>
</head>
<body>
<script>
/*
将newFruit数组里面的值复制到fruit数组里面,
生成一个新的数组。['apple', 'banana', 'pear']
*/
const fruit = ['apple', 'banana', 'pear','orange', 'maongo'];
const newFruit = ['orange', 'maongo'];
// //你可能会使用:push()方法
// fruit.push(newFruit);
// console.log(fruit)//(6) ["apple", "banana", "pear", "orange", "maongo", Array(2)]
// //当你使用push()方法的时候你发现,并没有我们想的形式,这个时候我们需要apply()方法
// fruit.push.apply(fruit,newFruit);
// console.log(fruit)//(7) ["apple", "banana", "pear", "orange", "maongo", "orange", "maongo"]
//其实没有必要这么麻烦,直接用...就可以替代apply()方法
fruit.push(...newFruit);
console.log(fruit)//(7) ["apple", "banana", "pear", "orange", "maongo", "orange", "maongo"]
/*
对于时间来说:年月日时分秒,扩展运算符也同样适用
*/
const dateFields=[2019,7,9];
//你可能会这样操作
const date=new Date(dateFields[0],dateFields[1],dateFields[2]);
console.log(date);//Fri Aug 09 2019 00:00:00 GMT+0800 (中国标准时间)
//现在你可以直接用扩展运算符来完成
const date1=new Date(...dateFields);
console.log(date1)//Fri Aug 09 2019 00:00:00 GMT+0800 (中国标准时间)
</script>
</body>
</html>