arguments 4

arguments 객체 - JavaScript

arguments 객체는 함수에 전달된 인수에 해당하는 Array 형태의 객체(유사배열)다. arguments 객체는 모든 함수 내에서 이용 가능한 지역 변수다. arguments 객체를 사용하여 함수 내에서 모든 인수를 참조할 수 있으며, 호출할 때 제공한 인수 각각에 대한 항목을 갖고 있다. 항목의 인덱스는 0부터 시작한다. //예를 들어 인수가 세 개인 함수일 경우 function f(a, b, c) { console.log(arguments[0]); console.log(arguments[1]); console.log(arguments[2]); } f('가', '나', '다') //가 //나 //다 arguments객체로 인수를 설정하거나 재할당 할 수도 있다. arguments[1] = 'new..

JavaScript 2020.11.27

Rest 파라미터(...args) - JavaScript

Rest 파라미터는 정해지지 않은 수(an indefinite number, 부정수)인 인수를 배열로 나타낼 수 있게 한다. 예시 function sum(...theArgs) { return theArgs.reduce((previous, current) => { return previous + current; }); } console.log(sum(1, 2, 3)); // expected output: 6 console.log(sum(1, 2, 3, 4)); // expected output: 10 //Rest 파라미터와 함께 다른 인수도 사용할 때 function myFun(a, b, ...manyMoreArgs) { console.log("a", a); console.log("b", b); conso..

JavaScript 2020.11.27