Default function parameter value in safari is not

2019-02-12 15:25发布

问题:

I am using angularjs, and having problem with null assignment in safari only. Its working in chrome and firefox.

$scope.submit = function(id=null){
}

I am getting this error

SyntaxError: Expected token ')'

when I remove null, It just works. I don't know why !

$scope.submit = function(id){
}

Please help me to understand why this happening only with safari ?

回答1:

Default parameters from ES2015 are not yet supported in Safari. Please check the compatibility table.

At the moment the basic support provide Chrome 49 and Firefox 15.


But you can use the following ES5 code to achieve the same:

$scope.submit = function(id) {
  if (id === undefined) {
    id = null;
  }
}