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

前端 未结 5 1654
臣服心动
臣服心动 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:10

    Javascript does not allow a "default" specifier.

    A quick way of doing what you would want is changing:

    function saveItem(andClose = false) {
    
    }
    

    to the following:

    function saveItem(andClose) {
        // this line will check if the argument is undefined, null, or false
        // if so set it to false, otherwise set it to it's original value
        var andClose = andClose || false;
    
        // now you can safely use andClose
        if (andClose) {
            // do something
        }
    }
    

提交回复
热议问题