JavaScript Arguments objects

Source: Internet
Author: User

1.arguments is of course only meaningful in the function body, ARGUMENTS.LENGTH returns the number of arguments passed in the function.

<script type= "Text/javascript" >
Window.onload = function () {
(Function (arg1, arg2) {
alert (arguments.length); 0
alert (arguments.callee.length);//2
})();
}
</script>

Arguments only makes sense in the function body, ARGUMENTS.LENGTH returns the number of arguments passed in the function, such as what I have not passed in here, but directly run an anonymous function, then the first alert must be ' 0 ', Again, Arguments.callee returns the function of the call itself, and for an anonymous function, it can be arguments.callee to get its own reference. Here Arguments.callee.length returns the number of arguments the function expects to pass in, so that the second alert is ' 2 ', if this is a function with a name, such as a function named MyTest, Then you can directly mytest.length to get the number of arguments that should be passed in.

2. The value of the formal parameter is always synchronized with the value in the arguments parameter array corresponding to one by one.

function Doadd (NUM1, num2) {
NUM1 = 10;
Alert (arguments[0]);
}
Doadd (5, 5); Get 10

Instead

function Doadd (NUM1, num2) {
Arguments[0] = 10;
Alert (NUM1 + num2);
}
Doadd (5, 5);//Get 15

Note: In strict mode, the above procedure is wrong, cannot rewrite the arguments value inside the function, will error.

All parameter passes are passed by value, not by reference.

3. Inside the function, you can call it in the form of an array of arguments parameters. This means that the named parameter is not required, just for convenience.

function say () {
Alert (Arguments[0] + ' say ' + arguments[1]);
}
Say (' Xiao ', ' hello ');

JavaScript Arguments objects

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.