1. 程式人生 > >javascript es6 引數擴充套件

javascript es6 引數擴充套件

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>es6</title>
</head>
<body>
<h2>
  ES6
</h2>
<script>
  function sum (x, y, z) {
    let total = 0;
    if (x) total += x;
    if (y) total += y;
    if (z) total += z;
    console.log(`total: ${total}`)
  }
  sum(8,'', 10)
  function sums (...m) {
    let total = 0;
    for (let i of m) {
      total += i;
    }
    console.log(`total: ${total}`);
  }
  sums(4, 8, 9, 10);
  let sum3 = (...m) => {
    let total = 0;
    for (let i of m) {
      total += i;
    }
    console.log(`total: ${total}`);
  };
  sum3(100, 200, 300);
  console.log(...[4, 9, 10, 5, 6]);
  // 合併
  let arr1 = [1, 3];
  let arr2 = [2, 4];
  console.log(`output: ${arr1.concat(arr2)}`);
  console.log([...arr1, ...arr2]);
  let [x, ...y] = [10, 20, 30];
  console.log(x);// 10
  console.log(y);// [20, 30]
  let [...a] = 'es6';
  console.log(a); //["e", "s", "6"]
</script>
</body>
</html>