扩展运算符在函数中的运用(将一个数组push到另一个数组中去、对于年月日时分秒操作)

<!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>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值