1. What is arguments?
Arguments is a built-in object in JavaScript. It is odd and often overlooked, but it is actually very important. All major js function libraries use the arguments object. Therefore, the agruments object must be familiar to javascript programmers.
All functions have their own arguments object, which includes the parameters to be called. It is not an array. If typeof arguments is used, 'object' is returned '. Although we can call arguments by calling the data method. For example, length and index methods. However, the push and pop objects in several groups are not applicable.
2. Create a flexible function
It seems that the argument object is very limited to use, but it is actually a very useful object. You can use an argument object to allow the function to call a variable number of parameters. There is a Formatting Function in Dean Edwards's base2 library, demonstrating this flexibility.
Copy codeThe Code is as follows: function format (string ){
Var args = arguments;
Var pattern = new RegExp ("% ([1-" + arguments. length + "])", "g ");
Return String (string). replace (pattern, function (match, index ){
Return args [index];
});
};
We provide a template string. You can use "% 1" to "% 9" to add a placeholder to the return value. Then, insert nine other parameters.
Copy codeThe Code is as follows: format ("And the % 1 want to know whose % 2 you % 3", "papers", "shirt", "wear ");
The above code will return: And the papers want to know whose shirt you wear ".
One thing we need to note is that when defining a function, we only specify one parameter, string. Javascript allows us to pass any number of parameters to a function, regardless of how this function is defined. The Arguments object is allowed.
3. Convert the arguments object into a real Array
Although the arguments object is not a real javascript array, we can easily convert it into standard data and then perform Array Operations.
Copy codeThe Code is as follows: var args = Array. prototype. slice. call (arguments );
Now the args variable contains a standard javascript Array object containing all the parameters of the function.
4. Create a function through the preset arguments object
The Arguments object allows us to execute all types of javascript methods. The definition of a makeFunc function is attached here. This function allows us to provide a function reference and all parameters of this function. It will return an anonymous function to call the function you specified, and also provide parameters attached to the anonymous function call.
Copy codeThe Code is as follows: function makeFunc (){
Var args = Array. prototype. slice. call (arguments );
Var func = args. shift ();
Return function (){
Return func. apply (null, args. concat (Array. prototype. slice. call (arguments )));
};
}
The first argument object provides makeFunc with reference to the function you want to call. It is removed from the arguments array. Then makeFunc returns an anonymous function to run the specified method.
The argument of the first application points to the range of function calls, mainly pointing to the key part of the function. We should first keep this as null. The second arguments is an array that converts this function to an arguments object. MakeFunc Concatenates the original array values to the arguments object and provides the arrays for anonymous and called functions.
You need to output a template that is always in the same position, so you don't have to always call the format function every time you reference the template. You can use the general functions of makeFunc to return the functions that can call format and then automatically supplement the template.
Copy codeThe Code is as follows: var majorTom = makeFunc (format, "This is Major Tom to ground control. I'm % 1 .");
You can call the majorTom function as follows:
Copy codeThe Code is as follows: majorTom ("stepping through the door ");
MajorTom ("floating in a most peculiar way ");
Every time you call majorTom, it will call both the format function and the first argument, the already written template. Then return
Copy codeThe Code is as follows: "This is Major Tom to ground control. I'm stepping through the door ."
"This is Major Tom to ground control. I'm floating in a most peculiar way ."
5. Create a function to reference itself
You may think this is cool, but there are more surprises about arguments. He also has other useful features: The callee method. Arguments. callee includes a function reference to create an argument object. So how should we use it?
The Arguments. callee method allows an anonymous function to easily point to itself.
Repeat is a function that carries a function reference and two numbers. The first number is the number of function calls, and the second number is the interval of each call, in milliseconds.
Copy codeThe Code is as follows: function repeat (fn, times, delay ){
Return function (){
If (times-> 0 ){
Fn. apply (null, arguments );
Var args = Array. prototype. slice. call (arguments );
Var self = arguments. callee;
SetTimeout (function () {self. apply (null, args)}, delay );
}
};
}
The Repeat function uses the arguments. callee method to obtain a reference from the variable self, pointing to the function that runs the original command. In this way, the anonymous function can be called again.
I have a super-Brief function that carries a string and executes the alert method.
Copy codeThe Code is as follows: function comms (s ){
Alert (s );
}
However, I want to create a special version. With this version, I can repeat this action three times, with each interval of 2 seconds. So, we can
Copy codeCode: var somethingWrong = repeat (comms, 3, 2000 );
SomethingWrong ("Can you hear me, major tom ?");
The result of calling the somethingWrong function is to repeat this action three times, with each alert interval of 2 seconds.
Although Arguments is not often used, it is a bit odd, but it is full of surprises and deserves our understanding.