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);
   }
  }
})
1

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.