JavaScript Base-arguments Object

Source: Internet
Author: User
Tags script tag

Arguments is the inner object of the function, it is the existence of an array of classes, the so-called class array is not an array of methods,        but can be subscript to access the internal elements, also has the length property. What is its role? The        actual arguments passed in when the function call is saved, the length property is used to know the number of parameters passed in.        For example, the function fn
function fn (ARG1,ARG2,ARG3,ARG4)        {            // at this time Arguments.length is not 4 because it is not determined            by the number of parameters when declaring the function // This is determined by the number of arguments passed in when FN is called .         }            fn (+/-);   Arguments.length is 3        fn (1);        Arguments.length is 1.

It is learned that the value of length is determined by the parameters passed by the calling function, not by the number of formal parameters when declaring the function.

Some ideas: Arguments is an object, object is an invariant existence, that is, living things, you can understand.
On the contrary, such as the number 2, it is fixed, the amount of memory is fixed, can not be changed. The object is obviously not
It is understandable that the arguments passed in when the function is called are not consistent with the number of arguments when the function is declared.
When called, no matter what the number of arguments passed in the function, the formal parameter will not have any complaints, the function will not lose its temper error, because it will eventually be saved in the arguments object. The formal parameter seems to be just a "vase"!

Then: In the non-strict mode, "use strict" is not used within the script tag;

By arguments you can modify the values of the parameters passed in, of course there is a common precondition, the parameter must exist, and the value is not a special value undefined
eg

functionfn1 (ARG1,ARG2,ARG3) {//arg1 value existsConsole.log (ARG1);//1Arguments[0] = 20;    Console.log (ARG1); // -            //Arg3 Value Special value undefined, because the function call is not passed in, then such a modification is invalidConsole.log (ARG3);//undefinedARGUMENTS[2] = 100;    Console.log (ARG3); //undefined//is still undefined, modified invalid} fn1 ();

The opposite: the value of the corresponding arguments can also be modified by formal parameters. The premise is also that the value of arguments is not a special value undefined

functionfn2 (ARG1,ARG2,ARG3) {//Arguments[0] value is present, and the value of Arguments[0] is modified by modifying the value of the Arg1Console.log (Arguments[0]);//1Arg1 = 20; Console.log (arguments[0]);// -            //Arguments[3] Value is a special value undefined, the function call is not passed in, modify the value of Arg3, Arguments[3] The value is not to be modifiedConsole.log (arguments[2]);//undefinedARG3 = 100; Console.log (arguments[2]);//undefined//is still undefined, modified invalid} fn2 ();

Then: If in strict mode, the above two kinds of changes do not take effect. In strict mode, the value of the formal parameter is independent of the value of the arguments, and there is no relationship. "You leave the plank bridge, I walk my Highway"!
(JavaScript Advanced Programming version 3rd PDF seems to be wrong.) )

Extension: Since arguments is a parameter object inside a function, the value of the corresponding formal parameter is modified in addition to the non-strict mode
Is there any other role?
There are: overloads of the simulation functions. Because the function is not overloaded. Functions that follow the same name function will overwrite the previous function (which can be used to fill the brain).

Overloading: Different functions are implemented in the same function by the number of arguments in the incoming call

Eg: the overload of the analog function, the number of parameters is 1 when printing 1. Other situations Print 2

function fn3 (ARG1,ARG2)        {            if (arguments.length = = 1)            {                Console.log (1);            }             Else             {                console.log (2);            }        }

If I feel that there is some harvest, I feel very honored, if I feel that it is no more than that, then I will strive to improve their level of knowledge and expression, and if I feel that it can also guide twos, then I would be grateful, because I think the person who can guide you must have extraordinary place If you can give comments and point out the shortcomings, then I will be very happy, someone to help you so will be a very good thing!

JavaScript Base-arguments Object

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.