Understanding the arguments object in JavaScript parameters

Source: Internet
Author: User

The ECMAScript function does not have an attribute for the signature, so there is no overload in the ECMAScript function.

The presence of arguments in JavaScript can compensate for the lack of overloading of functions in JavaScript.

The parameters in a function in JavaScript are saved as an array, so when a function is called in JavaScript, the arguments passed by the function are not limited by the number of arguments we set when we define the function. That is, if we give a function to define 2 parameters, we pass the time can not pass, can pass one, can also pass two, pass three ... Can be, not affected by the number of function arguments. Arguments is similar to an array (but not a real array object), you can also use square brackets to access each of its values as an array, and use Arguments.length to determine the number of arguments.

It should be said that we can pass parameters to ECMAScript (JavaScript is the implementation of the ECMAScript standard) function arbitrarily, and can access these parameters through the arguments object.

Use of arguments:

function Myfun () {console.log (arguments.length);} Myfun ("1", "2");//2myfun ("1");//1myfun ();//0

function Myfun (num1,num2) {if (arguments.length = = = 1) {Console.log (arguments[0]);} else if (arguments.length = = 2) { Console.log (Arguments[0]+arguments[1]);} else if (arguments.length = = = 3) {Console.log (num1+num2+arguments[2]);} else {Console.log (arguments[0]+ "" +num1);}} Myfun (1);//1myfun (//3myfun);//6myfun ();//undefined  undefined

Named parameters can be used with arguments

Unlike other OO languages, in ECMAScript, all parameters are passed as values, and it is not possible to pass parameters by reference (after all, the values of the parameters are saved in arguments).

The callee property is also defined in the arguments object to refer to the currently executing function, for example, in recursion:

function factorial (num) {if (num <=1) {return 1;} else {return Num*arguments.callee (num-1);}} Console.log (factorial (5));//120

Understanding the arguments object in JavaScript parameters

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.