JavaScript arguments Object--The actual parameters of the function

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.

So, in JavaScript, the arguments object is the actual parameter of the function, and below, we come into this magical country and get a glimpse of it.

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)
{
throw new 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 the values, that is, change all the values of both
function change (param)
{
//For example, I preach param for Simaopig, Then alert is Simaopig,
//If nothing is passed, alert undefined
alert (param);
//With arguments[0] changed the value of this parameter
arguments[0] = ' Xiaoxiaozi ';
//Yes, this value becomes Xiaoxiaozi
alert (param);
The callee property of }

arguments: The callee property of

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

Remember the function that I used in my last article to implement recursive functions with functions that are defined directly in the volume?

Inside, I mentioned that you can use the function name for the function directly. This implementation of recursion can be very convenient to call themselves.

Now this callee with arguments can also be implemented in a simple way
//using the function direct amount, using the Arguments.callee property to implement the recursive function
var result = function (x) {
if (x<=1) return 1;
return X*arguments.callee (x-1);
};

at the end of the note, since this arguments is so powerful, let's not name the variable arguments, in fact arguments is one of the reserved words of JavaScript. Well.

JavaScript Arguments Object--The actual parameters of the function

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.