🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
Function对象的length属性用于返回该函数定义的参数个数。 该属性属于Function对象,所有主流浏览器均支持该属性。 ## 语法 ``` functionObject.length ``` ## 返回值 length属性的值为Number类型,返回当前函数在定义时声明的参数个数。 当创建一个函数的实例时,函数的length属性由JavaScript引擎初始化为该函数定义中的参数数目。 ## 示例&说明 ``` function test(){ document.writeln("本函数定义的参数个数为" + test.length); }; test(); // 本函数定义的参数个数为0 function foo(a, b){ document.writeln("本函数定义的参数个数为" + foo.length); }; foo(1, 2); // 本函数定义的参数个数为2 function bar(a, b, c, d){ document.writeln("本函数定义的参数个数为" + bar.length); } // length属性只与定义函数时的参数个数有关,与调用时传入的参数个数无关 bar(); // 本函数定义的参数个数为4 // 也可在函数外部直接调用 document.write(foo.length); // 2 ```