Because of language design errors, arguments can be treated as an array.
The code copy is as follows:
function zero () {
console.log(arguments[0]);
}
There will be
The code copy is as follows:
function zero () {
for(var i=0;i<arguments.length;i++){
console.log(arguments[i]);
}
}
It takes advantage of the fact that Javascript is that Javasc
The arguments variable here provides an array-like interface for the actual arguments. Because of the variable parameters of arguments here, we can use this interesting thing to do some interesting things, such as overloading.
Javscript reload
There is a question about overloading on stackvoerflow, so the first answer is
The code copy is as follows:
if (typeof friend === "undefined") {
} else {
}
Another answer is
The code copy is as follows:
switch (arguments.length) {
case 0:
//Probably error
break;
case 1:
//Do something
break;
case 2:
default: //Fall through to handle case of more parameters
//Do something else
break;
}
But this method is really not good-looking. Will our function eventually become like this?
The code copy is as follows:
function zero1 (){
console.log('arguments 1')
};
function zero2 (){
console.log('arguments 2')
};
function zero () {
if(arguments.length == 1){
zero1();
} else{
zero2();
}
}
It's really not good-looking at all. Even if we change the switch...case, it won't look good.
Javascript arguments are not an array
arguments are not always an array as we see, and sometimes it may not.
The code copy is as follows:
function hello(){
console.log(typeof arguments);
}
Here the type of arguments is an object, although the type of array is also an object, although we can convert it into an array
The code copy is as follows:
var args = Array.prototype.slice.call(arguments);
But this also shows that this is not an array, it has only the only property of Array, i.e. length. In addition to this
arguments.callee
Reference to the currently executing function.
arguments.caller
Reference to the function that invoked the currently executing function.
arguments.length
Reference to the number of arguments passed to the function.