arguments에 대해서

1.자바스크립트에서 함수를 호출할때 인수들이 암묵적으로 배열형태로 전달된다

// 일반적 사용
function show() {
    console.log(arguments)
    console.log(arguments.length)
    console.log(arguments[0])
}
show(1,2,3)
// [1,2,3]
// 3
// 1

  1. ES6의 화살표함수에선 arguments를 사용할수없기 때문에 ... 를 사용하여 아래와 같이 사용한다 (args라는 키워드가 있는건 아님)

// ES6의 화살표 함수
const func1 = (...args)=>{
    console.log(args)
}
 
func1(1, 2, 3);
// [1,2,3]

3.실제 사용 예제

아래와 같이 ajax콜의 응답을 arguments로 받아서 사용한적이있음

$.when( $.ajax( "/page1.php" ), $.ajax( "/page2.php" ) ).done(function( arguments ) {
  var data = arguments[ 0 ] + arguments 0 ];
});

※참조

https://boycoding.tistory.com/21

https://bubobubo003.tistory.com/55

https://api.jquery.com/jquery.when/

Feb 13, 2023 Views 82