问题:在JavaScript中定义带有可选参数的函数的正确方法是什么?
例如:
function myFunc(optionVar1) {
if(optionVar1 == undefined) {
...
} else {
...
}
}
myFunc('10'); // valid function call
myFunc(); // also a valid function call
Run Code Online (Sandbox Code Playgroud)
?在函数声明中使用像Ruby这样的标记是否合适,以表示可选参数:
function myFunc(optionVar1?) {...} // <--- notice the ? mark
Run Code Online (Sandbox Code Playgroud)