Arguments definition
All functions have a arguments object that stores the arguments it actually receives, rather than the list of parameters defined when the function declaration is used. It is not an array but resembles an array, with an array-like access character and way, which can be accessed by arguments[n] to the value of the corresponding individual parameter and has the length of the array. But there are some methods that do not have an array. You can convert arguments into a real array by call and then manipulate the array.
var args = Array.prototype.slice.call (arguments);
Class Array
1. Judge arguments is not an array
Alert (arguments instanceof Array);
Alert (arguments instanceof Object);
2. How to Strictly judge a data is an instance of an array class
function IsArray (value) {
if (typeof Array.isarray = = "function") {return
Array.isarray (value);
} else{return
Object.prototype.toString.call (value) = = "[Object Array]";
}
}
3. Convert arguments into groups
Method One: Built-in types can find built-in property methods via prototype, Array.prototype.slice is the built-in method to access Array slice. Returns an array through the slice method. Call is a method of calling an object, replacing the current object with another object.
var arg = Array.prototype.slice.call (arguments,0);
Method Two: The performance of the method is nearly the same as it is to create an array before the
var arg = [].slice.call (arguments,0);
Method Three: To transform the array by circulation
function ToArray (arguments) {
var a = [];
for (Var i=0;i<arguments.length;i++) {
A.unshift (arguments.[ I]);
return A;
}
Caller
When a function is called by another function, the called function automatically generates a caller property, points to the function object that called it, and caller null if the function is not invoked.
function Testcaller () {
var caller = Testcaller.caller;
alert (caller);
}
function Acaller () {
testcaller ();
}
Acaller ();
The pop-up is the content of the function Acaller.
Arguments.callee
Arguments.callee points to the running function itself, returning the functions object being executed, which is the body of the specified function object.
Note: The arguments.length is the actual parameter length, the arguments.callee.length is the parameter length, and is usually used to determine whether the shape participates in the argument length consistently
The parameters of the function are obtained by arguments, and the formal parameters of the function are obtained by Arguments.callee.
It is also widely used in closures.
var i = 0;
function B (num) {
if (num <) {
num++;
i++;
If there are parameters, callee also take the parameters;
Arguments.callee (num);
} else {
//output 2 times
alert ("Call" +i+ "secondary callee!");
}
B (8);
The application of Arguments.callee in closures provides a function of recursive flirting.
//Use Arguments.callee to compute factorial of 10, for example: 1x2x3x4x5x6x7 ....
function C (x) {return
x > 1 x * Arguments.callee (x-1): 1
} (a);
Output 6
alert (c (3));
Output 3628800
alert (c (10));
Example: Callee to seek 1-n and
function fn (n) {
if (n==1) return n;
else return N+arguments.callee (n-1);
}
It allows an anonymous function to call itself
Cases:
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>";
return result;
}
var listhtml = list ("O", "one", "two");
Console.log (listhtml);
Example 2: Interview question: The following console.log result is [1,2,3,4]?
function foo (x) {
console.log (arguments);
return x;
}
Foo (1,2,3,4);
function foo (x) {
console.log (arguments);
return x;
} (1,2,3,4)
The function fn () {} (1) is processed separately, divided into two functions, the first is the function fn () {}, and the second is the anonymous function: (1). If the second one does not have an argument, the error occurs, but the function above is contained in one (), then it is correct.
(function fn () {
console.log (arguments);
} (1,2,3,4));
(function foo (x) {
console.log (arguments);
return x;
}) (1,2,3,4)
function foo () {
bar.apply (null,arguments);
}
function bar (x) {
console.log (arguments);
}
Foo (1,2,3,4);