[1,2,3]. length can get 3, "123" . length can also get 3, and those who understand js a little know this.
But what will eval. length , RegExp. length , "".toString. length , 1..toString. length ?
Get 1, 2, 0, 1 respectively. What do these numbers represent?
In fact, the length of the function results in the number of formal parameters.
Let's take a brief example:
function test(a,b,c) {}test.length // 3function test(a,b,c,d) {}test.length // 4Isn't it very simple, but there are also special ones. If the function calls parameters through arguments without actually defining the parameters, length will only get 0.
function test() { console.log( arguments );}test.length // 0This 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.
function test() { console.log( arguments.length );}test(1,2,3); // Output 3test(1,2,3,4); // Output 4Therefore, the length property of a function can only obtain the number of its formal parameters, but cannot know the number of real parameters.