현재 번역은 완벽하지 않습니다. 한국어로 문서 번역에 동참해주세요.
The arguments object는 array비슷한 object인데 함수에 어떤 인자를 넣는가에 따라 object도 변화가 있습니다.
Syntax
arguments
Description
The arguments object는 함수안에서 지역변수처럼 쓰입니다. 즉 함수 밖에서는 못 쓴다는 말입니다.
함수의 인자들을 arguments object를 이용해서 함수 안에서 가리킬 수 있습니다. 이 arguments object는 각각의 성분들을 index를 이용해서 사용할 수 있습니다. 가장 첫번째 성분은 인덱스 0입니다. 만약 3개의 인자를 함수에 넘겼다면 그것을 다음과 같이 가리킬 수 있습니다:
arguments[0] arguments[1] arguments[2]
The arguments can also be set:
arguments[1] = 'new value';
The arguments object는 Array는 아닙니다. 배열이랑 비슷하긴 한데 배열이 가지고 있는 properties를 가지고 있지 않습니다 하지만 length성분은 가지고 있습니다. array가 아니라는 한가지 예는 pop method를 못 쓴다는 것입니다. 그러나 arguments object도 진짜 배열처럼 변할수는 있습니다:
var args = Array.prototype.slice.call(arguments);
Important: 위에 나온 slice를 arguments에 대해 쓰지 않는 것이 좋아요. 왜냐면 이것은 자바 스크립트 엔진의 최적화를 방해하기 때문입니다.(V8 for example). 대신에 arguments object에 대해 반복문을 써서 새로운 array로 만드는 것이 좋습니다. More information.
If Array generics are available, one can use the following instead:
var args = Array.slice(arguments);
The arguments object는 오직 함수 몸체안에서만 쓸 수 있습니다. 밖에서 쓰면 에러납니다.
arguments object는 함수호출시 정해진 인자보다 인자를 더 넣었을 때 요긴하게 쓸 수 있습니다. 정해진 숫자를 벗어난 함수 인자숫자에 대해서도 쓸 수 있으니까 유용하죠. arguments.length는 함수에 몇개에 인자가 넘겨졌는지 알 수 있게 하고 각각의 arguement를 arguments를 잘 이용하면 다 쓸 수 있습니다. (함수가 정의될 때 인자의 수를 결정하고 싶으면 , use the Function.length property.)
Properties
arguments.callee- 현재 실행되고 있는 함수를 가리킴.
arguments.caller- Reference to the function that invoked the currently executing function.
arguments.length- 함수에 넘겨진 인자의 숫자를 가리킴.
Examples
몇개의 문자열을 합치는 함수
separator인자는 문자열들을 구분하는데 사용되는 문자열(ex) , ; :)
function myConcat(separator) {
var args = Array.prototype.slice.call(arguments, 1);
return args.join(separator);
}
정해진 인자는 separator 하나지만 그냥 뒤에 myConcat('', ", "abc", "def") 이런식으로 인자 더 넣으면 문자열이 합쳐진다. 인자 여러개 넣을 수 있다는 말:
// returns "red, orange, blue"
myConcat(", ", "red", "orange", "blue");
// returns "elephant; giraffe; lion; cheetah"
myConcat("; ", "elephant", "giraffe", "lion", "cheetah");
// returns "sage. basil. oregano. pepper. parsley"
myConcat(". ", "sage", "basil", "oregano", "pepper", "parsley");
HTML lists를 만드는 함수
This example defines a function that creates a string containing HTML for a list. The only formal argument for the function is a string that is "u" if the list is to be unordered (bulleted), or "o" if the list is to be ordered (numbered). The function is defined as follows:
function list(type) {
var result = "<" + type + "l><li>";
var args = Array.prototype.slice.call(arguments, 1);
result += args.join("</li><li>");
result += "</li></" + type + "l>"; // end list
return result;
}
You can pass any number of arguments to this function, and it adds each argument as an item to a list of the type indicated. For example:
var listHTML = list("u", "One", "Two", "Three");
/* listHTML is:
"<ul><li>One</li><li>Two</li><li>Three</li></ul>"
*/
Rest, default and destructured parameters
The arguments object can be used in conjunction with rest parameters, default parameters or destructured parameters.
function foo(...args) {
return arguments;
}
foo(1, 2, 3); // { "0": 1, "1": 2, "2": 3 }
However, in non-strict functions, a mapped arguments object is only provided if the function does not contain any rest parameters, any default parameters or any destructured parameters. For example, in the following function that uses a default parameter, 10 instead of 100 is returned:
function bar(a=1) {
arguments[0] = 100;
return a;
}
bar(10); // 10
Specifications
| Specification | Status | Comment |
|---|---|---|
| ECMAScript 1st Edition (ECMA-262) | Standard | Initial definition. Implemented in JavaScript 1.1 |
| ECMAScript 5.1 (ECMA-262) The definition of 'Arguments Object' in that specification. |
Standard | |
| ECMAScript 2015 (6th Edition, ECMA-262) The definition of 'Arguments Exotic Objects' in that specification. |
Standard |
Browser compatibility
| Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
|---|---|---|---|---|---|
| Basic support | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) |
| Feature | Android | Chrome for Android | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
|---|---|---|---|---|---|---|
| Basic support | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) |

