3

when running the app that I have build on angular/phonegap I get the following error

Uncaught SyntaxError: Unexpected token =

The error is caused by the default value supplied for the parameter.But why is that?It works perfectly fine on a few devices and throws this error on others. Here is the code:

.factory('foo' , function(){
  return {
   test : function(id = 0){
     console.log(id);
   }
  }
 })

Here is the solution :

.factory('foo' , function(){
  return {
   test : function(id){
    id  = ( id !== undefined ) ? id : 0;
    console.log(id);
   }
  }
})
nikksan
  • 3,341
  • 3
  • 22
  • 27
  • your solution has issues with values that evaluate to falsey, read this answer for more details http://stackoverflow.com/a/6486334/4963159 – Michele Ricciardi Aug 25 '16 at 16:58

1 Answers1

2

Default parameters are a fairly new ECMAScript feature that was only added in the ECMAScript 2015.

The reason that it works on some devices but not others is down to the fact that they may have different browsers, or different versions of the same browser.

Only the latest browsers would be able to interpret and compile your code with ECMAScript 2015 features.

To get around this issue, if you want to use the latest specs without worrying about checking which features are available on which browsers, consider using a transpiler such as Babel in your build pipeline.

Michele Ricciardi
  • 2,107
  • 14
  • 17