arguments definition
All functions have their own arguments object to store the parameters it actually receives, without being limited to the parameter list defined when the function is declared. It is not an array but is similar to an array. It has the same access properties and methods as an array. It can access the values of the corresponding single parameter by arguments[n] and have the array length attribute length. But there are some methods that do not have arrays. You can convert arguments into real arrays through call, and then perform array operations.
var args = Array.prototype.slice.call(arguments);
Class array
1. Determine whether ARGUMENTS is an array
alert(arguments instanceof Array); alert(arguments instanceof Object);
2. How to strictly determine that a data is an instance of an array (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 to an array
Method 1: Built-in types can be found through prototype. Array.prototype.slice is the built-in method slice to access Array. Return an array through the slice method. call is a method that calls an object and replaces the current object with another object.
var arg = Array.prototype.slice.call(arguments,0);
Method 2: The performance is worse than method 1, because it creates an array first and then proceeds.
var arg = [].slice.call(arguments,0);
Method 3: Convert to an array through loop
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 will automatically generate a caller attribute, pointing to the function object that calls it. If the function is not called, the caller is null.
function testCaller() { var caller = testCaller.caller; alert(caller);}function aCaller() { testCaller();}aCaller();What pops up is the content of the function aCaller.
arguments.callee
arguments.callee points to the running function itself and returns the function object being executed, that is, the body of the specified Function object.
Note: arguments.length is the length of the actual parameter, arguments.callee.length is the length of the formal parameter, which is usually used to determine whether the length of the actual parameter is consistent.
Obtain the actual parameters of the function through arguments, and obtain the formal parameters of the function through arguments.callee.
It is also widely used in closures.
var i = 0; function b(num) { if (num < 10) { num++; i++; //If there are parameters, callee should also bring the parameters; arguments.callee(num); } else { //Output 2 alert("The call"+i+" callee!"); } } b(8); The application of Arguments.callee in closures provides a function of recursively calling. //Use arguments.callee to calculate the factorial of 10, for example: 1×2×3×4×5×6×7.... function c(x) { return x > 1 ? x * arguments.callee(x - 1) : 1 } (10); //Output 6 alert(c(3)); //Output 3628800 alert(c(10));Example: callee finds the sum of 1-n
function fn(n){ if(n==1) return n; else return n+arguments.callee(n-1);}It allows an anonymous function to call itself
example:
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: What is the following console.log result [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)During pre-explanation, function fn(){}(1); will be processed separately and divided into two functions, the first is function fn(){}, and the second is an anonymous function: (1). If the second one does not have parameters, an error will be reported, but the above function is included in a(), which 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);