Help with JS and functions parameters

后端 未结 6 1737
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-31 19:26

Does JS support two functions with the same name and different parameters ?

function f1(a, b)
{
// a and b are numbers
}

function f1(a, b, c)
{
// a is a string         


        
6条回答
  •  离开以前
    2021-01-31 19:40

    No, you can't use function overloading in JS.

    But, you can declare just the version with 3 parameters, and then check whether the third argument === undefined, and provide differentiated behaviour on that basis.

提交回复
热议问题