-
미정의 매개변수를 배열로 받는 나머지 매개변수 (Rest parameter)Javascript 2020. 4. 12. 18:23
Rest parameter
나머지 매개변수는 정해지지 않은 수(an indefinite number, 부정수)를 배열로 나타냅니다.
function restFuc(a, b, ...theArgs) { // ... }
함수의 마지막 파라미터 앞에 ... 을 붙여서 모든 나머지 인수를 배열로 대체합니다.
마지막 파라미터만 "Rest 파라미터"가 될 수 있습니다.
function restFuc(a, b, ...theArgs) { console.log("one", a) console.log("two", b) console.log("rest", theArgs) } restFnc(1, 2, 3, 4, 5) //console //one, 1 //two, 2 //rest, [3, 4, 5]
rest 파라미터는 Array 인스턴스로 sort, map, forEach 또는 pop 같은 메서드가 바로 인스턴스에 적용 될 수 있습니다.
rest 파라미터의 해체
Rest 파라미터는 데이터가 구분된 변수로 해체될 수 있습니다.
function restFuc(...[a, b, c]) { return a + b + c; } restFnc(1) // NaN restFnc(1, 2) // 3 restFnc(1, 2, 3, 4) // 6 (네번째 파라미터는 해체되지 않음)
https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Functions/rest_parameters
'Javascript' 카테고리의 다른 글
Runtime & Node.js (0) 2020.04.27 (loading)this (0) 2020.04.15 클로저 (Closure) (0) 2020.04.02 데이터 타입 (data type) (0) 2020.04.02 재귀함수 (0) 2020.03.30