The length attribute of JavaScript function, functionlength

Source: Internet
Author: User
Tags javascript array

The length attribute of JavaScript function, functionlength

[123, 3]. length can get 3, "". length can also get 3, which is known for js.

But what does eval. length, RegExp. length, "". toString. length, 1... toString. length get?

What do the numbers 1, 2, 0, and 1 represent?

In fact, the length of the function gets the number of parameters.

Let's take a simple example:

function test(a,b,c) {}test.length // 3function test(a,b,c,d) {}test.length // 4

It is not very simple, but it is also special. If the function calls the parameter through arguments, but does not actually define the parameter, the length will only get 0.

function test() { console.log( arguments );}test.length // 0

This function can indeed pass in parameters, and also call the parameters internally, but length cannot know the number of input parameters.
The number of real parameters can only be obtained through arguments. length during function execution.

Function test () {console. log (arguments. length);} test (, 3); // output 3 test (,); // output 4

Therefore, the length attribute of the function can only obtain itsParametersNumber, but cannot knowReal ParametersNumber.



The length attribute of javascript Functions is different from that of arguments.

Arguments is a special attribute of an object. 1: arguments is a copy of the received real parameter. in lexical analysis, the AO attribute is first formed based on the form parameter. The value is undefined. When the real parameter is sent, then modify the corresponding attributes of AO. 2: Collect all received arguments and put them in an arguments object t (a, B, c) {}. the call time is t (, 5) five parameters. At this time, the AO attribute has only three attributes: a, B, c, and. arguments contains 1, 2, 3, 4, 5, all values, length, and obtains the length of the arguments object.

How to use the property length of the javascript array object?

When defining an array, Parentheses () are used. Do not use [].
In a for loop, if you want to assign a value to the defined a [] array, you should write it
A [I] = ...;
Nothing else.

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.