Detailed description of javascript built-in object arguments

Source: Internet
Author: User
This article mainly introduces the javascript built-in object arguments. The example explains how to use arguments. If you need it, refer to section 1 and 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.

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.

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.

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.

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.

var majorTom = makeFunc(format, ”This is Major Tom to ground control. I'm %1.”);

You can call the majorTom function 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

“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.

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.

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

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.

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.