What is arguments
arguments It's a built-in object in JavaScript, it's weird and often overlooked, but it's really important. All major JS libraries take advantage of the arguments object. So agruments objects are necessary for JavaScript programmers to be familiar with.
All functions have a arguments object of their own, which includes the parameters to be called by the letter. He is not an array, and if you use typeof arguments, the return is ' object '. Although we can call arguments with the method of invoking the data. such as length, and the index method. However, the push and pop objects of the array are not applicable.
Create a flexible function
It looks like the argument object is very limited to use, but it's actually a very useful object. You can use the argument object to allow the function to invoke a variable number of arguments. There's a formatted function in Dean Edwards's Base2 library that shows this flexibility.
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 have provided a template string that you can add a placeholder to the return value with "%1" to "%9". Then provide nine additional parameters to insert.
Format ("and the%1 want to know whose%2%3″," papers "," shirt "," wear ");
The above code will return: And the papers want to know whose shirt you wear" .
One thing we need to be aware of is that when defining a function, we only specify a parameter, string. JavaScript allows us to pass any number of arguments into a function, regardless of how the function is defined. The arguments object is allowed for these.
Convert the arguments object to a real array
Although the arguments object is not a true JavaScript array, we can easily convert it to standard data and then manipulate the array.
var args = Array.prototype.slice.call (arguments);
Now this variable, args, contains a standard JavaScript array object with all the parameters of the function.
Creating a function from a arguments object preset
The arguments object allows us to execute all types of JavaScript methods. Here is a definition of a makefunc function. This function allows us to provide a function reference and all the parameters of the function. He will return an anonymous function to invoke the function you specified, as well as provide the parameters that accompany the anonymous function call.
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 gives Makefunc a reference to the function you want to invoke. He was removed from the arguments array. Makefunc then returns an anonymous function to run the specified method.
The argument of the first application points to the scope of the function call, mainly to the key part inside the function. Let's keep this as null first. The second arguments is an array that transforms the function into a arguments object. Makefunc concatenates the original array values into an array of arguments objects provided to the anonymous function and the called function.
You need to output a template that is always the same location so that you don't always have to call the format function every time you reference a template. You can use the general functionality of Makefunc to return functions that can call format and then automatically supplement the template.
var majortom = makefunc (format, "This was Major Tom to ground control. I ' m%1. ");
You can call the MajorTom function like this:
MajorTom ("Stepping Through the door");
MajorTom ("floating in a Most peculiar");
Every time you call MajorTom, it calls the Format function and the first argument, the template that has been written. Then the return
Major Tom to ground control. I ' m stepping through the door. "
Major Tom to ground control. I ' M floating in a the most peculiar. "
Create a function that references itself
You might think it's cool, but there are more surprises in arguments. He also has other useful features: the callee method. Arguments.callee includes a reference to a function to create a argument object. So how do you use it?
The Arguments.callee method makes it easy for an anonymous function to point to itself.
Repeat is a function that hosts a function reference and two numbers. The first number is a function call several times, the second number is the interval of each call, in milliseconds.
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 get a reference from the variable self, pointing to the function that runs the original instruction. This allows the anonymous function to call itself again.
I have a super-intro function that hosts a string and executes the alert method.
function comms (s) {
alert (s);
}
However, I would like to create a special version that I can repeat this action three times, each interval 2 seconds. Well, we can
var somethingwrong = Repeat (comms, 3, 2000);
Somethingwrong ("Can You Hear me, Major Tom?");
The result of calling the Somethingwrong function is to do this action three times, each time the alert interval is 2 seconds.
Arguments, though not often used, is a bit odd, but it's full of surprises and is worth knowing.
Original address: arguments : A JavaScript Oddity
Source: http://www.cnblogs.com/Fskjb/archive/2011/10/27/2227111.html
JavaScript arguments (GO)