La proprietà length
indica il numero di parametri che la funzione si aspetta di ricevere.
The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.
Property attributes of Function.length |
|
---|---|
Writable | no |
Enumerable | no |
Configurable | yes |
Description
length
è una proprietà di un oggetto Function
che indica quanti argomenti la funzione si aspetta, cioè il numero di parametri formali. Questo numero esclude il rest parameter e include solo i parametri prima del primo con un valore predefinito. Al contrario, arguments.length
è locale a una funzione e fornisce il numero di argomenti effettivamente passati alla funzione.
Data property of the Function constructor
Il costruttore Function
è esso stesso un oggetto Function
. La sua proprietà length
ha valore 1. Gli attributi delle proprietà sono: Writable: false
, Enumerable: false
, Configurable: false
.
Property of the Function prototype object
La proprietà length
del prototype di un oggetto Function
ha valore 0.
Examples
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
Specifications
Specification | Status | Comment |
---|---|---|
ECMAScript 1st Edition (ECMA-262) | Standard | Definizione iniziale. Implementata in JavaScript 1.1. |
ECMAScript 5.1 (ECMA-262) The definition of 'Function.length' in that specification. |
Standard | |
ECMAScript 2015 (6th Edition, ECMA-262) The definition of 'Function.length' in that specification. |
Standard | Gli attributi configurabili di queste proprietà diventano true . |
ECMAScript (ECMA-262) The definition of 'Function.length' in that specification. |
Living Standard |
Browser compatibility
BCD tables only load in the browser