Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Default function parameter value in safari is not working

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 ?

like image 873
GRESPL Nagpur Avatar asked Apr 25 '16 13:04

GRESPL Nagpur


People also ask

Is it possible to give a default value to a function parameter?

Default function parameters allow named parameters to be initialized with default values if no value or undefined is passed.

How do you call a function with default parameters?

If a function with default arguments is called without passing arguments, then the default parameters are used. However, if arguments are passed while calling the function, the default arguments are ignored.

Which kind of parameters Cannot have a default value?

An IN OUT parameter cannot have a default value. An IN OUT actual parameter or argument must be a variable.

What is the correct syntax for declaring a default parameter?

Default function parameters allow formal parameters to be initialized with default values if no value or undefined is passed. function foo(a, b) { a = typeof a !==


1 Answers

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;
  }
}
like image 85
Dmitri Pavlutin Avatar answered Sep 30 '22 14:09

Dmitri Pavlutin