js function.length和function.arguments.length区别
2013年10月15日
function.length代表函数定义的参数个数
function.arguments.length函数实际传递的参数个数
如下:
function recursive10to2Int(n)
{
window.alert(recursive10to2Int.length);
window.alert(recursive10to2Int.arguments.length);
}
recursive10to2Int();
recursive10to2Int(1);
recursive10to2Int(1,2);
function.length
Description
length
is a property of a function object, and indicates how many arguments the function expects, i.e. the number of formal parameters. This number does not include the rest parameter. By contrast, [arguments.length](https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Functions_and_function_scope/arguments/length "JavaScript/Reference/Functions_and_function_scope/arguments/length")
is local to a function and provides the number of arguments actually passed to the function.
Example
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 /