[JavaScript]'...' three dot 함수, 'array[i] || []' 함수

Ronie🌊·2021년 1월 21일
0

JavaScript😹

목록 보기
2/2
post-thumbnail

'...' three dot 함수
'array[i] || []' 함수


'...' three dot 함수

  • Rest Parameter
    배열이기 때문에 arguments 객체를 배열로 변환할 필요없이 바로 argument의 배열에 JavaScript의 Array instance method를 적용할 수 있다는 점에서 유용하게 쓰일 수 있습니다.
function sumWithRest(... args) {
    for(let i = 0 ; i < args.length; ++i) console.log(args[i]);
    return args.reduce((acc, cur) => acc + cur);
}
sumWithRest(1, 2, 3, 4);
  • Destructuring rest parameters
    함수 안에서 argument들의 배열을 array destructuring 한 효과를 얻을 수 있습니다.
function destructuringParam(... [a, b, c]) {
    console.log(a, b, c);
}
destructuringParam(1, 2, 3);  
  • Spread Operator
let ar = ['hello', 'nello'];
let ar2 = ['dello', 'bello']

console.log(ar);
console.log(...ar);
console.log([...ar, ...ar2]);
//[ 'hello', 'nello' ]
//hello nello
//[ 'hello', 'nello', 'dello', 'bello' ]

'array[i] || []' 함수

x에 값이 있으면 x에 할당하거나 빈 배열 []로 초기화합니다.

var x = x || [];

0개의 댓글