【ECMAScript 5_6_7】7、ES6——点点点运算符

一、点点点运算符

* 用途
1. rest(可变)参数
    * 用来取代arguments(函数内部用来收集实参的伪数组) 但比 arguments 灵活,只能是最后部分形参参数
    function fun(...values) {
        console.log(arguments);
        arguments.forEach(function (item, index) {
            console.log(item, index);
        });
        console.log(values);
        values.forEach(function (item, index) {
            console.log(item, index);
        })
    }
    fun(1,2,3);
2. 扩展运算符
  let arr1 = [1,3,5];
  let arr2 = [2,...arr1,6];
  arr2.push(...arr1);
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>07_3点运算符</title>
</head>
<body>
<script type="text/javascript">
  function add(a,b) {
    console.log(arguments)
    // arguments.callee() //该方法指向该函数本身,用来递归调用
    /*arguments.forEach(function (item,index) {  // 这里报错,因为arguments是伪数组,不能使用数组的方法
      console.log(item,index)
    })*/
  }
  add(10,20)

  function add1(a,b,...value) {  // ...只能收集最后部分形参参数,且必须处于最后一个
    console.log(value)
    value.forEach(function (item,index) {
      console.log(item,index)
    })
  }
  add1(1,2,3,4,5)

  let arr = [1,6]
  let arr1 = [2,3,4,5]
  console.log(...arr1)  // ...自动遍历自身参数
  arr = [1,...arr1,6]
  console.log(arr)

</script>

</body>
</html>

猜你喜欢

转载自blog.csdn.net/edc3001/article/details/85963132
今日推荐