JavaScript Built-in object Arguments Details _ basics

Source: Internet
Author: User
Tags anonymous javascript array

One, what is arguments
Arguments is a built-in object in JavaScript, it's quirky and often overlooked, but it's really important. All the major JS libraries use the arguments object. So agruments objects must be familiar to JavaScript programmers.
All functions have a arguments object of their own, which includes the parameters to be invoked by the function. He is not an array, and if you use typeof arguments, it returns ' object '. Although we can call the arguments using the method that invokes the data. such as length, and the index method. However, the push and pop objects of the array are not applicable.
Second, create a flexible function
It looks like the argument object is very limited in use, but it's actually a very useful object. You can use the argument object to enable the function to invoke an indefinite number of arguments. There's a format function in Dean Edwards's Base2 library that shows this flexibility.

Copy Code 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 that you can use "%1" to "%9" to add a placeholder to the return value. Then provide nine additional parameters for insertion.

Copy Code code as follows:
Format ("and"%1 want to know whose%2 for you%3″, "Papers", "shirt", "wear");

The code above will return: and the papers want to know whose shirt.
One thing we need to be aware of is that when we define a function, we only specify one argument, string. JavaScript allows us to pass any number of arguments into a function, regardless of how this function is defined. The arguments object is allowed for these.
Third, convert the arguments object into a true array
Although the arguments object is not a true JavaScript array, we can easily convert it to standard data and then do an array operation.
Copy Code code as follows:
var args = Array.prototype.slice.call (arguments);

So now the variable args contains a standard JavaScript array object that contains all the parameters of the function.
Iv. creating functions from preset arguments objects
The arguments object allows us to execute all types of JavaScript methods. Here is a definition of the Makefunc function. This function allows us to provide a function reference and all the parameters of this function. He will return an anonymous function to invoke the function you specified, as well as the parameters that came with the anonymous function call.
Copy Code 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 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 the key part of the function. Let's leave this as null first. The second arguments is an array that converts the function to a arguments object. Makefunc concatenates the original array values into an array of arguments objects that are supplied to anonymous functions and called functions.
You need to output a template that is always the same location, so you don't always have to call the format function every time you reference the template. You can use Makefunc's common function to return functions that can call format and then automatically supplement the template.

Copy Code code as follows:
var majortom = makefunc (format, "This are Major Tom to ground control.") I ' m%1. ');

You can call the MajorTom function like this:

Copy Code code as follows:
MajorTom ("Stepping Through the door");
MajorTom ("floating in a Most peculiar way");

Every time you call MajorTom, it calls both the Format function and the first argument, a template that has already been written. Then it will return
Copy Code 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. "

V. Create a function that references itself
You might think it's cool, but arguments has more surprises. He 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 a two-digit number. The first number is a function call several times, and the second number is the interval of each call, in milliseconds.
Copy Code code 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 a function that runs the original instruction. This way, the anonymous function can call itself again.
I have a super profile function that hosts a string and executes the alert method.

Copy Code code as follows:
function comms (s) {

alert (s);

}

However, I would like to create a special version, through this version I can repeat this action three times, each interval of 2 seconds. Well, we can

Copy Code code as follows:
var somethingwrong = Repeat (comms, 3, 2000);
Somethingwrong ("Can You Hear me, Major Tom?");

The result of calling the Somethingwrong function is to duplicate this action three times, each alert interval of 2 seconds.
Arguments Although not often used, a bit eccentric, but it is full of surprises, it is worth our understanding.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.