arguments 객체

CODEDRAGON Development/JavaScript, jQuery, ...

반응형

   

arguments 객체 구성

구성 요소

설명

함수 호출 시 넘겨진 인자(배열 형태)

함수를 호출할 때 인자는 인덱스 번호로 저장됩니다.

(첫 번째인자는 0번 인덱스, 두번째는 인자는 1번 인덱스)

callee 프로퍼티

현재 실행중인 함수의 참조값

length 프로퍼티

호출할 때 넘어온 인자의 개수.

배열과 유사하게 동작하지만, 배열이 아니므로 배열 메소드를 사용할 경우 에러가 발생합니다.

   

소스 코드

<html>

<head>

<meta charset="UTF-8">

<title>arguments 객체</title>

<script type="text/javascript">

//add() 함수

function sum(arg1, arg2) {

// arguments 객체 출력

console.dir(arguments);

return arg1+arg2;

}

document.write(sum(1) + '<br>');

document.write(sum(1,2) + '<br>');

document.write(sum(1,2,3) + '<br>');

</script>

</head>

<body>

</body>

</html>

   

출처: <https://github.com/10zeroone/study_javascript/blob/master/WebContent/ch04-function/29.html>

   

arguments객를 출력하여 함수 인자가 달라짐에 따라 이 객체의 값이 어떻게 달라지는 확인할 수 있습니다.

   

Arguments[1]

  1. 01
  2. callee: function sum(arg1, arg2) {
  3. length1
  4. __proto__: Object

Arguments[2]

  1. 01
  2. 12
  3. callee: function sum(arg1, arg2) {
  4. length2
  5. __proto__: Object

Arguments[3]

  1. 01
  2. 12
  3. 23
  4. callee: function sum(arg1, arg2) {
  5. length3
  6. __proto__: Object

   

함수 호출 시 넘겨진 인자(배열 형태)

Arguments[1] 1

Arguments[2] 1,2

Arguments[3] 1,2,3

callee 프로퍼티

Arguments[1] 2

Arguments[2] 3

Arguments[3] 4

현재 실행중인 함수의 참조값을 의미하며 여기서는 sum( )함수가 해당됨

length 프로퍼티

Arguments[1] 3

Arguments[2] 4

Arguments[3] 5

   

반응형

'Development > JavaScript, jQuery, ...' 카테고리의 다른 글

메소드 호출시 this바인딩  (0) 2014.08.12
arguments 객체 이용  (0) 2014.08.08
매개변수  (0) 2014.08.04
함수 객체의 length 프로퍼티  (0) 2014.08.01
함수 객체의 length 프로퍼티  (0) 2014.07.27