如何仅传递第二个参数的值,而函数在JS中具有三个参数?

发布于 2025-02-13 05:57:21 字数 130 浏览 1 评论 0原文

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

在这里,我希望10作为“端”值。 “启动”和“步骤”的默认值为0和1。

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

Here I want 10 as the "end" value. Default values of "start" and "step" are 0 and 1.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

满天都是小星星 2025-02-20 05:57:21
function range({start=0, end, step=1}) {

}

console.log(range({step: 10}))
function range({start=0, end, step=1}) {

}

console.log(range({step: 10}))
ˉ厌 2025-02-20 05:57:21

未定义的(从字面上看)值为默认值。

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

A default value is taken for undefined (literally) value.

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

谁许谁一生繁华 2025-02-20 05:57:21

我找到了两种做到这一点的方法,

function range(start=0,end, step=1) {}
console.log(range(10,null,1)) 

function range({start=0, end, step=1}) {}
console.log(range({}))
console.log(range({start: 10}))
console.log(range({start: 10,step:10})) 

第二种方法更容易自定义

I found two ways to do it

function range(start=0,end, step=1) {}
console.log(range(10,null,1)) 

function range({start=0, end, step=1}) {}
console.log(range({}))
console.log(range({start: 10}))
console.log(range({start: 10,step:10})) 

the second one is easier to custom

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文