In-depth parsing of arguments objects in JavaScript and in-depth parsing of javascript
Arguments Definition
All functions have their own arguments object, which is used to store the parameters it actually receives, not limited to the list of parameters defined during function declaration. It is not an array but similar to an array. It has the same access nature and method as an array. arguments [n] can be used to access the value of a single parameter and has the length attribute. But it does not have some methods of arrays. You can use call to convert arguments into a real array and then perform Array Operations.
var args = Array.prototype.slice.call(arguments);
Class Array
1. Determine if ARGUMENTS is an array
alert(arguments instanceof Array);alert(arguments instanceof Object);
2. How to strictly judge whether a data is an ARRAY instance
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: You can use prototype to find the built-in attribute method. Array. prototype. slice is the built-in method to access Array slice. The slice Method returns an array. A 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 performs
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 automatically generates a caller attribute pointing to the function object that calls the function. If the function is not called, the caller is null.
function testCaller() { var caller = testCaller.caller; alert(caller);}function aCaller() { testCaller();}aCaller();
The content of the aCaller function is displayed.
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 real parameter, and arguments. callee. length is the length of the form parameter. It is usually used to determine whether the length of the form in the real parameter is consistent.
Obtain the real parameters of the function through arguments, and obtain the form parameters of the function through arguments. callee.
Closures are also widely used.
Var I = 0; function B (num) {if (num <10) {num ++; I ++; // if there is a parameter, callee should also include the parameter; arguments. callee (num);} else {// output 2 times alert ("called" + I + "callee! ") ;}} B (8); application of Arguments. callee in the closure, which provides a recursive call function. // Use arguments. callee calculates 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 ));
For example, callee calculates 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: Which of the following console. log results 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)
In the pre-explanation, function fn () {} (1); is processed separately and divided into two functions. The first one is function fn (){}, the second is an anonymous function: (1 ). If the second parameter is not included, an error is returned, 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);