Javascript Functions and default parameters, not working in IE and Chrome

前端 未结 5 1655
臣服心动
臣服心动 2020-12-01 02:22

I created a function like this:

function saveItem(andClose = false) {

}

It works fine in Firefox

In IE it gives this error on the

5条回答
  •  不知归路
    2020-12-01 03:06

    In your case, you have an other alternative to be sure that your variable is a boolean:

    function saveItem(andClose) {
      var andClose = true == andClose;
      // ...
    }
    

    Default value is undefined, and true == undefined => false, so your default value will be false :)

提交回复
热议问题