As we all know, there has a parameter which represents variables in JS function —— arguments. And when I was looking for something about "arguments", I found it was also a little bit complex, especially in some details. Therefore I would like to code some examples and also to write a blog to record them.
1. Normal case
The "arguments" is an array, and we can use it to represent the variables of function. For example as follows, the "arguments[0]" represents "num1", the "arguments[1]" represents "num2". And we can also use "arguments.length" to find how many variables are given to the function.
(function test(num1, num2){
alert(num1 === arguments[0]); // true;
})(1,2);
2. The second case
It would be difficult to get the variables when the number of actual variables is more than initial. In this case, we have to use "arguments" to help us. For example:
(function test(num1, num2){
alert(arguments[2]);
})(1,2,3);
In this example, we can't use normal methods to get the third varibale but using "arguments[2]". In return, if the actual variables are less than initial, the "arguments[1]" and "num2" will be "undefined".
(function test(num1, num2){
alert(arguments[1]); // undefined
alert(num2); // undefined
})(1);
How about changing the value of varibales? Let's look at an example, when we change the value of "num1", the "argument[0]" will change its value, too. And it will be the same when we change the value of "arguments[0]".
(function test(num1){
num1 = 2;
alert(arguments[0]); // 2;
})(1);
However, the "arguments" will not change if the variables don't get values initially.
(function test(num1, num2){
num2 = 2;
alert(arguments[1]); // undefined;
})(1);
What's more, as we all know, the length of array can be changed, so how about "arguments"? Can its length be changed? No. It doesn't work. Let's see an instance:
(function test(num1, num2){
alert(num2); // 2;
arguments.length = 1;
alert(num2); // 2;
})(1,2);
So we should remember that the length of "arguments" only dependes on the number of initial variables.
3. The third case
The finally situation is that when we use the strict mode, the "arguments" can't be change, for example:
(function test(num1, num2){
"use strict"
arguments[1] = 3;
alert(num2); // 2;
alert(arguments[1]); // 3;
num2 = 4;
alert(num2); // 4;
alert(arguments[1]); // 3;
})(1,2);
Therefore, when we use strict mode, maybe the "arguments" will not equal to the initial variables.
(That's all)