• <small id='r2NQK'></small><noframes id='r2NQK'>

  • <legend id='r2NQK'><style id='r2NQK'><dir id='r2NQK'><q id='r2NQK'></q></dir></style></legend>

    1. <i id='r2NQK'><tr id='r2NQK'><dt id='r2NQK'><q id='r2NQK'><span id='r2NQK'><b id='r2NQK'><form id='r2NQK'><ins id='r2NQK'></ins><ul id='r2NQK'></ul><sub id='r2NQK'></sub></form><legend id='r2NQK'></legend><bdo id='r2NQK'><pre id='r2NQK'><center id='r2NQK'></center></pre></bdo></b><th id='r2NQK'></th></span></q></dt></tr></i><div id='r2NQK'><tfoot id='r2NQK'></tfoot><dl id='r2NQK'><fieldset id='r2NQK'></fieldset></dl></div>
      <tfoot id='r2NQK'></tfoot>
        <bdo id='r2NQK'></bdo><ul id='r2NQK'></ul>

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

        Is there a better way to do optional function parameters in JavaScript?(有没有更好的方法在 JavaScript 中执行可选函数参数?)

        <legend id='xj6aP'><style id='xj6aP'><dir id='xj6aP'><q id='xj6aP'></q></dir></style></legend>
          <tfoot id='xj6aP'></tfoot>

            • <bdo id='xj6aP'></bdo><ul id='xj6aP'></ul>
              <i id='xj6aP'><tr id='xj6aP'><dt id='xj6aP'><q id='xj6aP'><span id='xj6aP'><b id='xj6aP'><form id='xj6aP'><ins id='xj6aP'></ins><ul id='xj6aP'></ul><sub id='xj6aP'></sub></form><legend id='xj6aP'></legend><bdo id='xj6aP'><pre id='xj6aP'><center id='xj6aP'></center></pre></bdo></b><th id='xj6aP'></th></span></q></dt></tr></i><div id='xj6aP'><tfoot id='xj6aP'></tfoot><dl id='xj6aP'><fieldset id='xj6aP'></fieldset></dl></div>
                  <tbody id='xj6aP'></tbody>

                <small id='xj6aP'></small><noframes id='xj6aP'>

                • 本文介绍了有没有更好的方法在 JavaScript 中执行可选函数参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我总是这样处理 JavaScript 中的可选参数:

                  I've always handled optional parameters in JavaScript like this:

                  function myFunc(requiredArg, optionalArg){
                    optionalArg = optionalArg || 'defaultValue';
                  
                    // Do stuff
                  }
                  

                  有没有更好的方法?

                  有没有这样使用 || 会失败的情况?

                  Are there any cases where using || like that is going to fail?

                  推荐答案

                  如果 optionalArg 被传递,你的逻辑将失败,但评估为 false - 试试这个作为替代方案

                  Your logic fails if optionalArg is passed, but evaluates as false - try this as an alternative

                  if (typeof optionalArg === 'undefined') { optionalArg = 'default'; }
                  

                  或其他成语:

                  optionalArg = (typeof optionalArg === 'undefined') ? 'default' : optionalArg;
                  

                  使用最能传达您意图的成语!

                  Use whichever idiom communicates the intent best to you!

                  这篇关于有没有更好的方法在 JavaScript 中执行可选函数参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Toggle HTML radio button by clicking its label(通过单击标签来切换 HTML 单选按钮)
                  Javascript how to change radio button label text?(Javascript如何更改单选按钮标签文本?)
                  JavaScript radio button confirmation(JavaScript 单选按钮确认)
                  How can I automatically select specific radio buttons with Greasemonkey?(如何使用 Greasemonkey 自动选择特定的单选按钮?)
                  AngularJs. Is it possible to deselect HTML “radio” input by click?(AngularJs.是否可以通过单击取消选择 HTML“收音机输入?)
                  Checking Value of Radio Button Group via JavaScript?(通过 JavaScript 检查单选按钮组的值?)

                    <tfoot id='g140e'></tfoot>
                        <tbody id='g140e'></tbody>
                        • <bdo id='g140e'></bdo><ul id='g140e'></ul>
                        • <legend id='g140e'><style id='g140e'><dir id='g140e'><q id='g140e'></q></dir></style></legend>

                          <i id='g140e'><tr id='g140e'><dt id='g140e'><q id='g140e'><span id='g140e'><b id='g140e'><form id='g140e'><ins id='g140e'></ins><ul id='g140e'></ul><sub id='g140e'></sub></form><legend id='g140e'></legend><bdo id='g140e'><pre id='g140e'><center id='g140e'></center></pre></bdo></b><th id='g140e'></th></span></q></dt></tr></i><div id='g140e'><tfoot id='g140e'></tfoot><dl id='g140e'><fieldset id='g140e'></fieldset></dl></div>

                          <small id='g140e'></small><noframes id='g140e'>