6.1 Function.prototype.length

参考以下文章学习:
1. Function.length - MDN

length 属性指明函数的形参(formal parameters)个数,length 是函数对象的一个属性,指该函数有多少个必须要传入的参数,即形参的个数,形参的数量不包括 rest parameter。仅包括第一个具有函数参数默认值的参数之前的参数个数。与之对比的是,arguments.length 是函数被调用时实际传参的个数。

Function.prototype.length 属性的属性描述符:

propertyDescriptor.png

var cl = console.log;

cl(Function.length); // expected output: 1
cl((function () {}).length); // expected output: 0
cl((function (a) {}).length); // expected output: 1
cl((function (a, b) {}).length); // expected output: 2 etc.

cl((function (...rest) {}).length); // expected output: 0. rest parameter is not counted;
cl((function (a, b = 1, c) {}).length); // expected output: 1. only parameters before the first one with a default value is counted
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容