SyntaxError: Unexpected token '='. Expected a ')' or a ',' after a parameter declaration.' in safari

If you have function like this Change these from

 function abc(a=2){
 }

To like this

 function abc(a){
  if(a === undefined){
    a = 2;
  }
 }

Safari not allow the assignment like this in function, in above first one.


I had this issue for the past two days and finally found a solution for my case. First, some back story to my particular issue:

Safari was throwing this error with no line number or stack trace, and it's a fatal error so it was killing the rest of the JS running on my page. The code was running fine in Chrome and Firefox, and after a long time attempting to debug by cutting and pasting certain parts of my code, I found the area which the issue was occurring in.

This was only half of the solution, but it was occurring when I was importing a ES6 class. It wasn't the import causing the issue, but the contents of the class were throwing an error at some point.

There were 4 possible classes this narrowed the search down to, as this particular class had 3 imports as well as it's own class body. I decided to, while still using Safari, to go to JSFiddle and paste the contents of each class into the fiddle and see which files compile, however it turns out JSFiddle also has code inspection capabilities and it picked up on my issue straight away.

What it came down to was I was utilising a proposed feature in ES6 which was not yet officially in the language, and only implemented by a select few browsers. This feature is class fields (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes/Class_fields).

Essentially, I was writing something similar to this:

class BrokenClass {

    constructor() { this.isShutdown = false; }

    shutdownGracefully() { ... }

    endProgram(shouldKill) {
        if (shouldKill) {
            this.isShutdown = true;
            return;
        }

        shutdownGracefully();
    }

    stop = () => this.endProgram(false); // An alias for ending gracefully
    kill = () => this.endProgram(true); // An alias for killing the program

}

I was writing the stop and kill methods of this class using the class fields syntax, as they were intended to be aliases and I believed it looked cleaner as it excluded writing braces for such a small amount of code. My main browser is Google Chrome, and as Chrome had implemented this proposal it worked perfectly through months of testing, until one company decided to use the system on a Mac OS using Safari.

As safari had not implemented this proposal, it caused the whole issue I described earlier about the fatal error with no context as to why it was occurring.

So, the solution to my problem, which will hopefully help someone else in this world, is to avoid using the Class Fields syntax unless you are using a tool such as Babel.


For anyone else that finds themselves here. I can confirm that in Safari version Version 12.1 (14607.1.40.1.4) you'll get the same error for using static variables in a class. For e.g

class MyClass {
  static aVariable = 'things';
}

P.S: Static getters seem fine

class MyClass {
  static get aVariable() { return 'things' };
}