Analysis of the length attribute _ basics of functions in javascript

Source: Internet
Author: User
The length attribute returns the number of characters in a string. In function, length is obtained as the number of parameters defined by a function. [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 represent?

This is a question that many new friends in the group have been asking. In fact, the length of the function gets the number of form parameters.
Let's take a simple example:

The Code is as follows:


Function test (a, B, c ){}
Test. length // 3

Function 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.

The Code is as follows:


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.

The Code is as follows:


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

Therefore, the length attribute of a function can only get the number of its form parameters, but cannot know the number of real parameters.

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.