1. What are arguments
arguments is a built-in object in JavaScript. It is weird and often overlooked, but it is actually very important. All major js function libraries utilize arguments objects. Therefore, the agruments object is necessary for javascript programmers to be familiar with.
All functions have their own arguments object, which includes the parameters to be called by the function. It is not an array, if typeof arguments are used, the return is 'object'. Although we can call arguments by calling data. For example, length, and index method. But the push and pop objects of arrays are not applicable.
2. Create a flexible function
It seems that the argument object is very limited to use, but in fact it is a very useful object. You can use the argument object to enable the function to call an indefinite number of parameters. There is a formatted function in Dean Edwards' base2 library that demonstrates this flexibility.
Copy the code 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 where you can add a placeholder to the return value using "%1" to "%9". Then provide nine other parameters to insert.
The code copy 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" .
There is something we need to pay attention to. When defining a function, we only specify one parameter, string. Javascript allows us to pass any number of parameters into a function, no matter how we define this function. Arguments objects are allowed for these.
3. Convert arguments object into a real array
Although the arguments object is not a real javascript array, we can still easily convert it into standard data and then perform array operations.
The code copy is as follows: var args = Array.prototype.slice.call(arguments);
Now this variable args contains a standard javascript array object containing all the parameters of the function.
4. Create functions through preset arguments objects
The Arguments object allows us to execute all types of javascript methods. Here is a definition of makeFunc function. 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 the parameters attached to the anonymous function call.
Copy the code 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 a reference to the function you want to call. He removed it from the arguments array. Then makeFunc returns an anonymous function to run the specified method.
The argument of the first application points to the scope of the function call, mainly pointed to by the key parts of the function. Let's keep this as null first. The second argument is an array that will be converted into an argument object for this function. makeFunc concatenates the original array values into the arguments object and the array of the called functions.
You need to output a template that is always the same position so that you don't always call the format function every time you refer to the template. You can use the general function of makeFunc to return functions that can call format and automatically supplement the template.
The code copy is as follows: var majorTom = makeFunc(format, "This is Major Tom to ground control. I'm %1.");
You can call the majorTom function like this:
The code copy is as follows: majorTom("stepping through the door");
majorTom("floating in a most peculiar way");
Every time you call majorTom, it will call the format function and the first argument at the same time, and the already written template. Then it will return
Copy the code 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 that references itself
You may think this is cool, but there are more surprises for arguments. He has other useful features: the callee method. Arguments.callee includes a reference to a function to create an argument object. So how to use it?
The Arguments.callee method allows an anonymous function to point to itself very conveniently.
Repeat is a function that carries a function reference and two numbers. The first number is how many times the function is called, and the second number is the interval between each call, in milliseconds.
The code copy 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 to point to the function that ran the original instruction. In this way, the anonymous function can be called again.
I have a super brief function that hosts a string and executes an alert method.
Copy the code as follows: function comms(s) {
alert(s);
}
However, I want to create a special version through which I can repeat this action three times, each time interval of 2 seconds. Then, we can
The code copy is as follows: 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 are not used frequently and are a bit weird, they are full of surprises and are worth learning from.