In Javascript Functions, there is an array object of classes named arguments. It looks so strange and the name is not passed, but many Javascript libraries use its powerful functions. Therefore, every Javascript programmer must be familiar with its features. Each function has a variable named arguments, which saves the currently called parameters in an array like an array. In fact, it is not an Array. If you use the typeof arguments statement to try to return "object" (object), it cannot use push and pop methods like Array. Even so, you can use the subscript and length attribute to get its value.
Compile flexible Functions
Although it seems that the name is not passed, it is true that arguments is a very useful object. For example, you can allow a function to process parameters of an indefinite number. In the base2 library written by Dean Edwards, a function called format makes full use of this feature:
The 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];
});
};
This function implements template replacement. You can use % 1 to % 9 in the place where dynamic replacement is to be made, and then replace the remaining parameters in sequence. For example:
Format ("And the % 1 want to know whose % 2 you % 3", "papers", "shirt", "wear"); the above script will return
"And the papers want to know whose shirt you wear ". Note that, even in the format function definition, we only define a parameter named string. Javascript, regardless of the number of parameters defined by the function, allows us to pass any number of parameters to a function and save these parameter values to the arguments object of the called function.
Convert to actual Array
Although the arguments object is not a real Javascript array, we can use the slice Method of the array to convert it into an array, similar to the following code
The Code is as follows:
Var args = Array. prototype. slice. call (arguments );
In this way, the array variable args contains the values of all arguments objects.
Create a function with preset Parameters
The arguments object can be used to briefly compile the Javascript code. The following is a function named makeFunc. It returns an anonymous function based on the name of the function you provide and any other number of parameters. When an anonymous function is called, it merges the previously called parameters and submits them to the specified function for running and returns the returned values.
The 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 parameter of makeFunc specifies the name of the function to be called (yes, there is no error check in this simple example), and the name will be deleted from args. MakeFunc returns an anonymous Function, which calls the specified Function using the Function Object apply method.
The first parameter of the apply method specifies the scope. Basically, the scope is the called function. However, in this example, it looks a little complicated, so we set it to null. The second parameter is an array, which specifies the parameter of the function to be called. MakeFunc converts its own arguments, connects the arguments of the anonymous function, and passes it to the called function.
In one case, there is always a template with the same output. To save every time we use the format function mentioned above and specify repeated parameters, we can use makeFunc. It returns an anonymous function and automatically generates the content after the specified template:
The Code is as follows:
Var majorTom = makeFunc (format, "This is Major Tom to ground control. I'm % 1 .");
You can specify the majorTom function as follows:
The Code is as follows:
MajorTom ("stepping through the door ");
MajorTom ("floating in a most peculiar way ");
When the majorTom function is called every time, it uses the first specified parameter to fill in the specified template. For example, the above Code returns:
The 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 ."
Self-referenced Function
You may think this is cool. Don't be in a hurry. There will be a bigger surprise in the future. It (arguments) also has another very useful property: callee. Arguments. callee contains the referenced object of the currently called function. So how can we use this stuff to do something? Arguments. callee is a very useful method to call its own anonymous functions.
The following is a function named repeat. Its parameters require a function reference and two numbers. The first number indicates the number of running times, while the second function defines the running interval (in milliseconds ). The following is the relevant code:
The 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 arguments. callee to obtain the current reference. After it is saved to the self variable, an anonymous function is returned to re-run the originally called function. Finally, use setTimeout and an anonymous function to implement delayed execution.
As a simple description, for example, you can write the following simple functions that provide strings and bring up a warning box in a common script:
The Code is as follows:
Function comms (s ){
Alert (s );
}
Now, I changed my mind. I want to compile a "special version" function that repeats three times and runs every two seconds. Using my repeat function, you can do this as follows:
The Code is as follows:
Var somethingWrong = repeat (comms, 3, 2000 );
SomethingWrong ("Can you hear me, major tom? ");
The result is as expected. Three warning boxes are displayed, with a delay of two seconds at a time.
Finally, even if arguments is not often used, it may seem a little strange, but its amazing features (not just those !) It is worth learning about.