javascript ES6之展開(Spread)、其餘(Rest)

by on # javascript

MDN - Spread

const people1 = ['悟空', '達爾', '克林'];
const people2 = ['幽助', '飛影', '藏馬'];

const merge1 = [people1, people2];
console.log(merge1);
//[["悟空", "達爾", "克林"], ["幽助", "飛影", "藏馬"]]

const merge2 = [...people1, ...people2];
console.log(merge2); //Spread
//["悟空", "達爾", "克林", "幽助", "飛影", "藏馬"]

const myFunction1 = function(){
  console.log(arguments);
  //arguments是類陣列,無法使用任何陣列的方法,箭頭函式不會有arguments  
  
  //類陣列轉換為陣列
  const arg = [...arguments];
  console.log(arg); //陣列  
}

myFunction1(1, 2, 3);

const myFunction2 = function(a, ...b){//其餘用b表示,b就是Rest
  console.log(a, b);
}

myFunction2(4, 5, 6, 7, 8, 9);

摘自 共筆

javascript, Spread, Rest
comments powered by Disqus