JavaScript函数带有默认参数和一个多重词

发布于 2025-02-07 06:12:36 字数 730 浏览 1 评论 0原文

假设我有这样的函数声明:

function manyParams(a, b=1, ...n) { ... }

现在很明显,我可以以一种可以提供ab的方式调用此函数在数组n下。

示例:

function manyParams(a, b=1, ...n) {
    console.log(JSON.stringify(a));
    console.log(JSON.stringify(b));
    console.log(n.length);
}

如果您使用以下参数调用此函数nyparams(1,1,“ John”,“ stacy”,“ Mike”,“ Bob”,“ Carly”);,您将获得此输出,这是有道理的:

1
1
5

但是,如果我想在b中使用默认值,在仍提供许多参数的同时,我如何指定“ john”是开始n?我认为我可以做这样的事情:nyparams(a = 1,n =“ john”,“ stacy”,“ mike”,“ bob”,“ carly”);,但这不是'工作。 运行这些参数为我提供以下输出:

1
"John"
4

Let's say I have a function declaration like this:

function manyParams(a, b=1, ...n) { ... }

Now clearly I can call this function in such a way that I can provide a, b, and then any number of additional arguments under an array n.

Example:

function manyParams(a, b=1, ...n) {
    console.log(JSON.stringify(a));
    console.log(JSON.stringify(b));
    console.log(n.length);
}

If you call this function with the following arguments manyParams(1, 1, "John", "Stacy", "Mike", "Bob", "carly");, you get this output, which makes sense:

1
1
5

However, what if I want to use the default value in b, while still providing many arguments, how do I specify that "John" is the start of n? I would think I could do something like this: manyParams(a=1, n="John", "Stacy", "Mike", "Bob", "carly"); but that doesn't work.
Running those arguments gives me the following output:

1
"John"
4

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文