[1,2,3].length can get 3, and "123".length can also get 3. Anyone who understands js knows this.
But what will eval.length, RegExp.length, "".toString.length, 1..toString.length get?
Get 1, 2, 0, and 1 respectively. What do these numbers represent?
This is a question that many new friends in the group have been asking. In fact, the length of the function is the number of formal parameters.
Let's take a brief example:
The code copy is as follows:
function test(a,b,c) {}
test.length // 3
function test(a,b,c,d) {}
test.length // 4
Isn't it very simple, but there are also special ones. If the parameter is called internally through arguments and no actual parameter is defined, length will only get 0.
The code copy is as follows:
function test() { console.log( arguments );}
test.length // 0
This function can indeed pass parameters, and parameters are also called internally, but length cannot know the number of parameters passed in.
The actual parameters can only be obtained through arguments.length when the function is executed.
The code copy 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 property of a function can only obtain the number of its formal parameters, but cannot know the number of real parameters.