如何在javascript函数中为参数的特定默认值设置值

mdln97

对不起,如果这是转帖,但我还没有找到答案

假设您有以下功能:

function calculus(a, b = 2, c = 3)  {
   return a + b - c; 
}

我想要做的是为参数 a 和 c 传递新值,而不必编辑 b

所以我写了我在以下代码段中尝试过的方法

function calculus(a, b = 2, c = 3) {
  return a + b - c;
}

document.body.append("Case 1: Result " + calculus(2))
document.body.appendChild(document.createElement("br"));
document.body.append("Case 2: Result " + calculus(2, 2, 2))
document.body.appendChild(document.createElement("br"));
document.body.append("Csse 3: Result " + calculus(2, c = 2))
document.body.appendChild(document.createElement("br"));
// next line syntax error
//document.body.append("Case 4:" + calculus(2, c : 3))

所以在函数调用“c=2”或“c:2”中写入不会产生与案例2相同的结果

有什么语法可以用来做到这一点吗?所以只传递 a 和 c 作为参数并省略 b,仍然得到与案例 2 相同的结果?

如果没有,您会使用哪些解决方法?也许是一个带有属性作为参数的对象?

连接器

只需传递undefined要省略的参数:

function calculus(a, b = 2, c = 3) {
  return a + b - c;
}

document.body.append("Case 1: Result " + calculus(2))
document.body.appendChild(document.createElement("br"));
document.body.append("Case 2: Result " + calculus(2, 2, 2))
document.body.appendChild(document.createElement("br"));
document.body.append("Case 3: Result " + calculus(2, undefined, 2))
document.body.appendChild(document.createElement("br"));
document.body.append("Case 4: Result " + calculus(2, undefined, 3))

您还可以使用允许[2,,3]传递的数组参数

function calculus([a, b = 2, c = 3]) {
  return a + b - c;
}

document.body.append("Case 1: Result " + calculus([2]))
document.body.appendChild(document.createElement("br"));
document.body.append("Case 2: Result " + calculus([2, 2, 2]))
document.body.appendChild(document.createElement("br"));
document.body.append("Case 3: Result " + calculus([2,,2]))
document.body.appendChild(document.createElement("br"));
document.body.append("Case 4: Result " + calculus([2,,3]))

一个对象会以同样的方式工作:

function calculus({a, b = 2, c = 3}) {
  return a + b - c;
}

document.body.append("Case 1: Result " + calculus({a: 2}))
document.body.appendChild(document.createElement("br"));
document.body.append("Case 2: Result " + calculus({a: 2, b:2, c:2}))
document.body.appendChild(document.createElement("br"));
document.body.append("Case 3: Result " + calculus({a: 2, c: 2}))
document.body.appendChild(document.createElement("br"));
document.body.append("Case 4: Result " + calculus({a: 2, c: 3}))

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在Javascript中为可选参数设置默认值?

如何在@RequestParam中将默认值设置为LocalDateTime

如何在CreateTables()上为列设置默认值?

如何确定函数参数是自动设置为默认值还是在C ++中显式设置?

在Lua中,如何将nil参数正确设置为某个默认值?

将默认参数发送为null并使用函数中设置的默认值

是否可以在PowerShell中的许多函数调用中为命名参数设置默认值?

如何为命名函数参数设置默认值?

如何在Scala中为varargs参数设置默认值?

如何将ActiveSheet,ActiveWorkbook或ThisWorkbook设置为VBA中可选参数的默认值?

如何在程序定义的架构中为参数设置默认值?

如果它们为空,如何在程序中设置日期输入参数的默认值?

如何在MySQL中将“ MONTHNAME()”函数设置为列默认值?

为Rcpp函数中的参数设置默认值NULL

使用AWS CLI时如何在JMESPath查询中为空值设置默认值?

如何在关键字中为可选列表参数设置默认值

如何记录为参数默认值

如何在SwiftUI的View函数中将View设置为默认值?

如何在Debian上将upstart设置为默认值?

在函数中设置List <string>类型的参数的默认值

如何在下拉JavaScript中设置默认值

C#-如何在组合框datagridview中的表中将值设置为默认值?

如何为通过JavaScript中的对象文字模拟的命名函数参数设置默认值?

如何在参数替换中将默认值设置为元素数组?

如何在Rails中为参数设置默认值?

如何将 proc 参数的默认值设置为 Tcl 中的新字典

如何在angularjs中为select设置默认值

如何在 Javascript 中为命名参数使用默认值

如何将昨天的值设置为 datastage 中数字参数的默认值?