函数长度

函数长度

一、只给形参(无默认值):

        function f0() { }
        console.log(f0.length)//0

        function f1(a) { }
        console.log(f1.length)//1

        function f2(a, b) { }
        console.log(f2.length)//2

        function f3(a, b, c) { }
        console.log(f3.length)//3
小结:

函数长度等于参数个数(没有默认值情况下)

二、添加默认值

        function f1(a = 0) { }
        console.log(f1.length)//0

        function f2(a, b = 0) { }
        console.log(f2.length)//1

        function f3(a, b, c = 0) { }
        console.log(f3.length)//2

        function f4(a, b = 0, c) { }
        console.log(f4.length)//1
小结:

函数的长度,就是第一个具有默认值之前的参数个数

三、特殊情况-剩余参数 ...args

        function f1(name, ...args) {}

        console.log(f1.length) // 1
小结:

剩余函数不计入函数长度内

posted @ 2022-07-17 18:56  Kangf  阅读(180)  评论(0编辑  收藏  举报