JS How length and the length function arguments get?

function a(x,y){}
a.length // 2
 
function b(x,y=2,z){}
b.length // 1
 
function c(x,...args){}
c.length //1
JS length is a function object attribute value, the value is referred to as "the function how many parameters must be passed", i.e., the number of parameter 
number of formal parameters do not include the number of remaining parameters, only including "a first number of parameters "have a default value before 

when no default value, fn.length refers to the parameter number, if the parameter has a default value, then take the first number with a default value of the parameter prior .

The number of function arguments should be how to get?
arguments.length // used within a function, it represents the actual number of parameters passed, irrespective calculated at run time, and the number of shape parameters

  example:

function a(x,y,z){
   console.log(arguments.length); // 3
}(1,2,3)
 
function b(x,y=2){
    console.log(arguments.length) // 3
}(1,2,3)
 
 
function c(x,y=2){
    console.log(arguments.length) // 1

}

  Conclusion is: function.length parameter is acquired and the length of the internal function is defined argument.length acquired argument length

Guess you like

Origin www.cnblogs.com/cxdong/p/11429445.html