【问题标题】:How to pass only 2nd parameter's value while function has three parameters in js?当函数在js中有三个参数时,如何只传递第二个参数的值?
【发布时间】:2022-07-04 20:45:17
【问题描述】:
function range(start=0,end, step=1) {

}
console.log(range(10))

这里我想要 10 作为“结束”值。 “start”和“step”的默认值为0和1。

【问题讨论】:

  • 顺便说一句,如果你只有两个参数,你怎么知道你是传递一个起始值还是一个步骤?顺便说一句,undefined(字面意思)值采用默认值。

标签: javascript function parameters arguments default-value


【解决方案1】:
function range({start=0, end, step=1}) {

}

console.log(range({step: 10}))

【讨论】:

    【解决方案2】:

    function range(start = 0, end, step = 1) {
        return [start, end, step];
    }
    console.log(range(undefined, 10));

    【讨论】:

      猜你喜欢
      • 2018-11-07
      • 1970-01-01
      • 2011-03-08
      • 1970-01-01
      • 1970-01-01
      • 2021-10-19
      • 1970-01-01
      • 2019-07-05
      • 2019-07-15
      相关资源
      最近更新 更多