" ... " 在JavaScript中的意思?


Posted by hoyi-23 on 2021-07-03

在ES6中,新增了一個"..."的關鍵字,這個關鍵字在不同時間點有不同的效果,有時候作為 其餘運算子 有時作為 展開運算子

展開運算子(spread operator)

展開運算子 通常用在 陣列 或是 函式的參數。

將陣列內的東西展開

const data = ['010','020','030'];
const arr = ['000',...data];
console.log(arr) // ['000',010','020','030']

用在函式參數

function sum(x, y, z) {
  return x + y + z;
}

const numbers = [1, 2, 3];

console.log(sum(...numbers));
// expected output: 6

其餘運算子(rest operator)

將陣列內的東西分離

console.log(arr) // ['000',010','020','030']

const [a,b,...others] = arr //解構賦值
console.log(a) //'000'
console.log(b) //'010'
console.log(others) //'020','030'

#其餘運算子 #Rest Operator #展開運算子 #Spread Operator #...







Related Posts

0 - 0.5 的實體伺服器架設

0 - 0.5 的實體伺服器架設

CSS 衍生的資安問題(上) - 初探 CSS Ingection

CSS 衍生的資安問題(上) - 初探 CSS Ingection

Vue.js 學習旅程Mile 6 – 資料單向綁定篇:v-text & v-html

Vue.js 學習旅程Mile 6 – 資料單向綁定篇:v-text & v-html


Comments