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.