gpt4 book ai didi

Javascript:仅在函数中使用特定参数

转载 作者:行者123 更新时间:2023-12-03 09:24:18 25 4
gpt4 key购买 nike

我认为这是一个初学者的问题:如何处理函数中的特定参数并忽略其他参数?

示例:

http://jsfiddle.net/8QuWj/

function test(para_1, para_2, para_3) {
if (para_3 == true) {
alert('only the third parameter was successfully set!');
}
}

test(para_3=true);

我想单独决定是否在函数中使用某个参数。

最佳答案

您可以使用 if(param) 单独检查每个参数,例如:

function test(para_1, para_2, para_3) {
if (para_1) {
// First parameter set
}
if (para_2) {
// Second parameter set
}
if (para_3) {
// Third parameter set
}
}

一般来说,你不能只设置一个参数并期望它是第三个,因为它会自动将其设置为第一个,而其余2个为未定义。因此,如果您想调用您的函数并且只有第三组,那么您很可能会这样做

test(null, null, 'this_is_set');

关于Javascript:仅在函数中使用特定参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24596027/

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