On the length property of function in JavaScript-basic knowledge

Source: Internet
Author: User

[1,2,3].length can get 3, "123". Length can also get 3, this slightly understand JS all know.

But Eval.length,regexp.length, "". tostring.length,1. What will tostring.length get?

Get 1,2,0,1, what do these numbers mean?

This is a group of new friends have been asked a question, in fact, the length of the function is the number of parameters.
Let's take a quick look at an example:

Copy Code code as follows:

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

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

is not very simple, but there are special, if the function inside is through arguments call parameters, without the actual definition of parameters, length will only get 0.

Copy Code code as follows:

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

This function can indeed pass in arguments, and internal parameters are called, but length does not know the number of arguments passed in.
The number of arguments can only be obtained by arguments.length when the function is executed.

Copy Code code as follows:

function test () {console.log (arguments.length);}
Test (1,2,3); Output 3
Test (1,2,3,4); Output 4

So the length property of a function can only get the number of his parameters, but not the number of arguments.

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.