问题:
This question already has an answer here: 这个问题在这里已有答案:
- Set a default parameter value for a JavaScript function 24 answers 为JavaScript函数设置 24个答案 的默认参数值
I've always handled optional parameters in JavaScript like this: 我总是在JavaScript中处理可选参数,如下所示:
function myFunc(requiredArg, optionalArg){
optionalArg = optionalArg || 'defaultValue';
// Do stuff
}
Is there a better way to do it? 有没有更好的方法呢?
Are there any cases where using ||
有没有使用||
like that is going to fail? 那会失败吗?
解决方案:
参考一: https://stackoom.com/question/cjd/有没有更好的方法在JavaScript中执行可选的函数参数-重复参考二: https://oldbug.net/q/cjd/Is-there-a-better-way-to-do-optional-function-parameters-in-JavaScript-duplicate
来源:oschina
链接:https://my.oschina.net/u/4428122/blog/4326710