The role of arguments in JS

Source: Internet
Author: User

In the JavaScript function body, the identifier arguments has a special meaning. It is a special property of the calling object that is used to refer to the arguments object. Arugments objects are like arrays, note that this is just like not ha.


In the JavaScript function body, arguments is like an array (not a real array, a arguments object, again) , with the length property, which can represent the number of arguments passed to the function.


The reference to a formal parameter can be either a parameter name or a arguments[] array, where Arguments[0] represents the first argument.


Arguments.length Properties:


JS will not take the initiative for you to determine how many parameters you give the function, if you pass more, the extra part is not used, if you pass less, then the parameter value is not passed undefined


So we can use the length property of arguments to detect if the function is called using the correct number of actual arguments, because JavaScript doesn't do it for you.


function f (x, Y, z)

{

First check that the number of parameters passed is correct

if (arguments.length!=3)

{

Thrownew Error ("function f called with" + arguments.length+ "arguments, but it is not 3 arguments.");

}

Run the real function below

}

Arguments also provides us with the possibility of passing an arbitrary number of actual parameters for a function:


For example, I want to judge the size of some of the numbers you pass to me, take out the biggest one, right, yes, you can pass as many arguments as you want, but only if you're going to pass numbers, because I'm too lazy to judge inside the function. Oh.


function Max ()

{

According to my previous log, this is the smallest number in JavaScript.

var m = number.negative_infinity;

for (var i =0; i < arguments.length; i++)

{

As long as any one parameter is larger than M, then m becomes the value of this parameter

if (arguments[i]> m)

m = Arguments[i];

}

return m;

}

What do you think? This is a clever method, isn't it? Oh.


Explain that the arguments is consistent with the formal parameters of true transmission:


For example, if you pass a parameter called Param to the function, and only this one parameter, then param and arguments[0] are references to this parameter value,


Change one of these values, that is, change all the values of both


function Change (param)

{

For example, I preach the param for Simaopig, then alert is Simaopig,

If nothing passes, it will alert undefined.

alert (param);

Change the value of this parameter with Arguments[0]

arguments[0]= ' Xiaoxiaozi ';

Yes, that's the value of Xiaoxiaozi.

alert (param);

}

Arguments properties of callee:


The callee property of arguments is used to refer to the currently executing function, which is very useful for unnamed function calls themselves.


Now with the arguments of this callee can also be easily implemented


The recursive function is implemented by using the Arguments.callee property with the direct amount of the function

var result =function (x) {

if (x<=1) return1;

Return X*arguments.callee (x-1);

};

At the end of the note, since this arguments is so powerful, we should not name the variable arguments, in fact arguments is one of the reserved words of JavaScript.

The role of arguments in JS

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.