有没有更好的方法在JavaScript中执行可选的函数参数? [重复]

泄露秘密 提交于 2020-07-29 09:58:26

问题:

This question already has an answer here: 这个问题在这里已有答案:

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
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!