gpt4 book ai didi

javascript - 将特定的预定义参数传递给函数

转载 作者:行者123 更新时间:2023-11-29 14:38:56 24 4
gpt4 key购买 nike

就这样吧:

let something = (a , b ,  c = 0 ,  d = 0 ,  e = 0) =>
{console.log(`a is ${a}; b is ${b};c is ${c};d is ${d};e is ${e}`)};`

如果我给一个函数两个以上的参数,它会分别接受它们。如果我想将值传递给“e”变量并跳过其他预定义的变量怎么办?

最佳答案

您可以将参数设为对象。这样做有两个好处,首先,您不必担心顺序,也不必将值传递给 c 或 d。

let something = ({a , b ,  c = 0 ,  d = 0 ,  e = 0}) => {
return `a is ${a}; b is ${b};c is ${c};d is ${d};e is ${e}`;
}
console.log(something({a: 12 , b: 13, e: 51})); //"a is 12; b is 13;c is 0;d is 0;e is 51"

关于javascript - 将特定的预定义参数传递给函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41315366/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com