JavaScript function's length property is described

  • 2020-03-30 03:54:52
  • OfStack

[1,2,3]. Length gives you a 3, and "123". Length gives you a 3.

But eval. Length, RegExp. Length, "".tostring.length, 1.. What do I get for tostring.length?

You get 1, 2, 0, 1. What do these Numbers represent?

The length of the function is actually the number of parameters.

Let's take a quick look at an example:


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

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

Isn't that simple, but it's also special that if the argument is called internally through arguments without actually defining the argument, the length will only get 0.


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

This function does pass in arguments, and it calls arguments internally, but length does not know the number of arguments passed in.
You can only get arguments from arguments. Length while the function is executing.


function test() { console.log( arguments.length );}
test(1,2,3); //The output of 3
test(1,2,3,4); //  The output  4

So the length property of a function gets only the number of arguments, not the number of arguments.


Related articles: