Length attribute of JavaScript function

Source: Internet
Author: User

Length attribute of JavaScript 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 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 // 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.

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.


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.