--- title: Function.length slug: Web/JavaScript/Reference/Global_Objects/Function/length translation_of: Web/JavaScript/Reference/Global_Objects/Function/length ---
La proprietà length
indica il numero di parametri che la funzione si aspetta di ricevere.
{{EmbedInteractiveExample("pages/js/function-length.html")}}
length
è una proprietà di un oggetto {{jsxref("Function")}} che indica quanti argomenti la funzione si aspetta, cioè il numero di parametri formali. Questo numero esclude il {{jsxref("rest_parameters", "rest parameter", "", 1)}} e include solo i parametri prima del primo con un valore predefinito. Al contrario, {{jsxref("Functions_and_function_scope/arguments/length", "arguments.length")}} è locale a una funzione e fornisce il numero di argomenti effettivamente passati alla funzione.
Il costruttore {{jsxref("Function")}} è esso stesso un oggetto {{jsxref("Function")}}. La sua proprietà length
ha valore 1. Gli attributi delle proprietà sono: Writable: false
, Enumerable: false
, Configurable: false
.
La proprietà length
del prototype di un oggetto {{jsxref("Function")}} ha valore 0.
console.log(Function.length); /* 1 */ console.log((function() {}).length); /* 0 */ console.log((function(a) {}).length); /* 1 */ console.log((function(a, b) {}).length); /* 2 etc. */ console.log((function(...args) {}).length); // 0, rest parameter is not counted console.log((function(a, b = 1, c) {}).length); // 1, only parameters before the first one with // a default value is counted
Specification | Status | Comment |
---|---|---|
{{SpecName('ES1')}} | {{Spec2('ES1')}} | Definizione iniziale. Implementata in JavaScript 1.1. |
{{SpecName('ES5.1', '#sec-15.3.5.1', 'Function.length')}} | {{Spec2('ES5.1')}} | |
{{SpecName('ES6', '#sec-function-instances-length', 'Function.length')}} | {{Spec2('ES6')}} | Gli attributi configurabili di queste proprietà diventano true . |
{{SpecName('ESDraft', '#sec-function-instances-length', 'Function.length')}} | {{Spec2('ESDraft')}} |
{{Compat("javascript.builtins.Function.length")}}