What is the length of an anonymous function?

Today I encountered a problem with anonymous functions:

(function() {}).length;    // 0
(function(a,b) {}).length; // 2

Why do anonymous functions have the same number of length and formal parameters?


the answer is here: length is an attribute value of a function object, indicating how many parameters the function must pass in, that is, the number of formal parameters.

`Function.length-JavaScript | MDN
https://developer.mozilla.org.`


has nothing to do with anonymity. Function.length indicates the number of formal parameters of the function


Both the

function and arguments have the length attribute,

The length of

arguments object mainly reflects the number of arguments,

The length of

function mainly reflects the number of formal parameters,

but since JavaScript 1.4, the arguments.length property has been discarded

Menu