Skip to content

Instantly share code, notes, and snippets.

@chuck0523
Created June 30, 2015 23:35
Show Gist options
  • Save chuck0523/177d52ba24e021a1c507 to your computer and use it in GitHub Desktop.
Save chuck0523/177d52ba24e021a1c507 to your computer and use it in GitHub Desktop.
//Rest Parameters
rest(1,2,3);
function rest(...args){
console.log(args); // [1, 2, 3]
console.log(arguments); // [1, 2, 3]
console.log(Array.isArray(args)); //true
console.log(Array.isArray(arguments)) //false
args.forEach(x => console.log(x)); //1 2 3
arguments.forEach(x => console.log(x)); //Error
}
function rest(x, y, ...others) {
console.log(x, y, others);
}
rest(1, 2, 3); // 1 2 [3]
//引数が渡らなかった場合は空の配列になる
rest(1); //1 undefined []
//最後の引数以外には使用できない
function rest(first, ...arr, last) { } //NG
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment