JavaScript built-in objects arguments detailed

Source: Internet
Author: User
Tags anonymous javascript array

  This article mainly introduces the JavaScript built-in object arguments, the example explains arguments is how to use, needs the friend to be possible to refer to under

One, what is arguments arguments is a built-in object in JavaScript, it is very strange, and often overlooked, but in fact is very 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 looks like 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 invoke an indefinite number of arguments. There's a format function in Dean Edwards's Base2 library that shows this flexibility.     Code as follows: function format (string) {     var args = arguments      var pattern = NE W RegExp ("% ([1-" + Arguments.length + "])", "G");      return string (string). replace (pattern, function (match, index) {       return arg S[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.     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. There's one thing we need to be aware of., when we define the 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 through preset Arguments objects 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. The code is as follows: function Makefunc () {     var args = Array.prototype.slice.call (arguments);      var f UNC = 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 outputA template is always the same location, so you don't always have to call the format function every time you reference a template. You can use Makefunc's common function to return functions that can call format and then automatically supplement the template.     Code is as follows: var majortom = makefunc (format, "This are Major Tom to ground control." I ' m%1. ');   You can call MajorTom functions like this:     code is 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 the copy code code is returned as follows: "This is Major Tom to ground control." I ' m stepping through the door. '    ' this are 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.   Code as follows: function repeat (FN, times, delay) {     return function () {       if –> 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 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.   Code as follows: function comms (s) {       alert (s);     }       However, I would like to create a special In this version, I can repeat this action three times, each interval of 2 seconds. Well, we can     copy code as follows: var somethingwrong = repeat (comms, 3, 2000);    Somethingwrong ("Can You Hear me, Major Tom"); The result of the   call to 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.