前幾天在IE 11 遇到這個問題 , javascript 預設參數b = 1 情況下瀏覽器顯示錯誤

In IE it gives this error on the console: Expected ')'

這是因為IE11 瀏覽器兼容性未支援ES6「 Parameters without defaults after default parameters

function multiply(a, b = 1) {

return a*b;

}

multiply(5); // 5

 

所以解法為了因應三大瀏覽器都可以使用建議用以下方式處理

function multiply(a, b) {

var b = typeof b !== 'undefined' ? b : 1;

return a*b;

}

multiply(5); // 5

 

引用:

https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Functions/
default_parameters

https://kangax.github.io/compat-table/es6/

http://blog.mozilla.com.tw/posts/7403/es6-series1

arrow
arrow

    衛斯理不理 發表在 痞客邦 留言(0) 人氣()