본문 바로가기

JavaScript/Modern JavaScript

[JavaScript] Arguments 객체

[Arguments 객체]

 

Arguments 객체는 함수를 호출할 때 전달한 아규먼트들을 배열의 형태로 모아둔 유사 배열 객체임. 함수를 호출할 때 전달되는 아규먼트의 개수가 불규칙적일 때 유용하게 활용함.

 

function printArguments() {
  // arguments 객체의 요소들을 하나씩 출력
  for (const arg of arguments) {
    console.log(arg); 
  }
}

printArguments('Young', 'Mark', 'Koby');

[줄임말 만들기]

 

1. arguments 객체를 이용한 for 반복문

 

function firstWords() {
  let word = '';
  
  // 여기에 코드를 작성하세요
  
  for (i = 0; i < arguments.length; i++) { 
    let cut = arguments[i][0];
    word = word + cut 
  }
  console.log(word);
}


firstWords('나만', '없어', '고양이'); // 나없고
firstWords('아니', '바나나말고', '라면먹어'); // 아바라
firstWords('만두', '반으로', '잘라먹네', '부지런하다'); // 만반잘부
firstWords('결국', '자바스크립트가', '해피한', '지름길'); // 결자해지
firstWords('빨간색', '주황색', '노란색', '초록색', '파란색', '남색', '보라색'); // 빨주노초파남보

 

2. for of 반복문

 

function firstWords() {
  let word = '';
  
  // 여기에 코드를 작성하세요

 for (const arg of arguments) {
   let cut = arg[0];
   word = word + cut;
 }
 console.log(word);
}

firstWords('나만', '없어', '고양이'); // 나없고
firstWords('아니', '바나나말고', '라면먹어'); // 아바라
firstWords('만두', '반으로', '잘라먹네', '부지런하다'); // 만반잘부
firstWords('결국', '자바스크립트가', '해피한', '지름길'); // 결자해지 
firstWords('빨간색', '주황색', '노란색', '초록색', '파란색', '남색', '보라색'); // 빨주노초파남보