美文网首页
函数参数默认值

函数参数默认值

作者: ticktackkk | 来源:发表于2020-11-02 14:23 被阅读0次

    基础用法

     function a(x = 1, y = 2) {
            console.log(`x=${x},y=${y}`);
          }
          a();  // x=1,y=2
          a(2,3)  // x=2,y=3
    

    使用默认值语法设置函数参数的默认值。

    // bad
    function handleThings(opts) {
      opts = opts || {};
    }
    
    // good
    function handleThings(opts = {}) {
      // ...
    }
    

    相关文章

      网友评论

          本文标题:函数参数默认值

          本文链接:https://www.haomeiwen.com/subject/xacmvktx.html