The explanation and usage of arguments object in JavaScript _javascript skills

Source: Internet
Author: User

One, arguments use method

Access to each element through the square brackets syntax

var fun = function (one) {
 console.log (arguments[0]);
 Console.log (Arguments[1]);
 Console.log (arguments[2]);
Fun (1, 2, 3)
//1
/2
//3

With length properties, see how many parameters you want

function Fun () {return
 arguments.length;
}
Fun (1, 2, 3)//3
Fun (1)//1
Fun ()//0

Parameter assignment ("not allowed in strict mode")

var fun = function (A, b) {
 arguments[1] = 2;
 return a + B;
}
Fun (1, 1)
//3

Ii. the relationship between arguments and array

Although arguments can use the bracket syntax and have length attributes, the arguments object is just like an array, not Array an instance. Therefore, you cannot use standard array methods for arguments variables, such as push, pop, or slice. Although for it is possible to use circular traversal, it is best to convert it to a true array for better use of the array method.

How do I use an array method?

By apply means of the method, arguments is passed as a parameter so that the arguments can use the array method.

For the Apply method
myfunction.apply (obj, arguments).
Use to merge Array.prototype.concat.apply with another array
([1,2,3], arguments)

The ultimate solution, directly into the real array

The following code creates a new array that contains the elements in all arguments objects.

var args = Array.prototype.slice.call (arguments);
or
var args = [];
for (var i = 0; i < arguments.length i++) {
 args.push (arguments[i]);
or
var args = [].slice.call (arguments, 0);

Third, powerful but not recommended callee properties

The arguments callee property can call the function itself, and it can be invoked when the function is executing, and recursive calls to the method are implemented

The following code will implement a method of finding the factorial

var factorial = function (x) {return
 X<=1?1:x*arguments.callee (x-1);
}

In ECMAScript5 strict mode, the read-write operation of this attribute produces a type error and can significantly affect the performance of the modern JavaScript engine, so it is strongly recommended that you do not use arguments.callee and its properties.

Iv. Summary

The above is the entire content of this article, I hope the content of this article for everyone's study and work can bring certain help, if you have questions you can message exchange.

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.