13

I've muddled some JS code together which appears to work in firefox (no errors and functions correctly) but throws up 'SyntaxError: Unexpected token '='. Expected a ')' or a ',' after a parameter declaration.' in safari. I have the following object in php

$items = Array ( [0] => stdClass Object ( [id] => 1 [class] => class_a [make] => Kia [model] => Picanto [features] => 3,4,5,6,7,8 [colour] => white [engine] => 1000cc [ordering] => 1 [published] => 1 [image] => images/vehicles/picanto.jpg ) [1] => stdClass Object ( [id] => 2 [class] => motorbike [make] => Honda [model] => Transalp [features] => [colour] => blue [engine] => 650cc [ordering] => 6 [published] => 1 [image] => ) [2] => stdClass Object ( [id] => 3 [class] => moped [make] => Sym [model] => SR [features] => [colour] => white [engine] => 150cc [ordering] => 5 [published] => 1 [image] => ) [3] => stdClass Object ( [id] => 4 [class] => class_b [make] => Suzuki [model] => Splash [features] => 12 [colour] => Red [engine] => 1300cc [ordering] => 3 [published] => 1 [image] => images/vehicles/suzuki_splash.jpg ) [4] => stdClass Object ( [id] => 5 [class] => class_f [make] => Peugot [model] => 307 Cabrio [features] => 8,9,10,11 [colour] => Black [engine] => 1600cc [ordering] => 4 [published] => 1 [image] => images/vehicles/peugeot307.jpg ) [5] => stdClass Object ( [id] => 6 [class] => class_a [make] => Hyundai [model] => Atos [features] => [colour] => white [engine] => 1100cc [ordering] => 2 [published] => 1 [image] => images/vehicles/atos.jpg ) )

And the following javascript

<script type="text/javascript">
jQuery(document).ready(function($){
    var items = <?PHP echo json_encode($items)?>;
    console.log(items);
    for (var i = 0, len = items.length; i < len; i++) {
        lookup[items[i].id] = items[i];     //access the new lookup object using lookup[id].variable i.e lookup[1].image
    }
    var vId ="";
    function updateImg(img, display=true){
        if (display == true){
            $("#vehicle-image").show(500);
        }else{
            $("#vehicle-image").hide(500);
        }

        $("#vehicle-image").attr("src", '/'+ img);

    }
    $('#jform_vehicle').on('change', function() {
        var vId = parseInt(($("#jform_vehicle").chosen().val()));
        console.log (vId);
        if (isNaN(vId) !==true){
            var img = lookup[vId].image;
            console.log ('img=' + img);
            updateImg(img);
        }else{
            console.log('not a number');
            var img = "";
            updateImg(img, false)
        }
    });
});
</script>

in the browser the json_encode line is as follows

var items = [{"id":"1","class":"class_a","make":"Kia","model":"Picanto","features":"3,4,5,6,7,8","colour":"white","engine":"1000cc","ordering":"1","published":"1","image":"images\/vehicles\/picanto.jpg"},{"id":"2","class":"motorbike","make":"Honda","model":"Transalp","features":"","colour":"blue","engine":"650cc","ordering":"6","published":"1","image":""},{"id":"3","class":"moped","make":"Sym","model":"SR","features":"","colour":"white","engine":"150cc","ordering":"5","published":"1","image":""},{"id":"4","class":"class_b","make":"Suzuki","model":"Splash","features":"12","colour":"Red","engine":"1300cc","ordering":"3","published":"1","image":"images\/vehicles\/suzuki_splash.jpg"},{"id":"5","class":"class_f","make":"Peugot","model":"307 Cabrio","features":"8,9,10,11","colour":"Black","engine":"1600cc","ordering":"4","published":"1","image":"images\/vehicles\/peugeot307.jpg"},{"id":"6","class":"class_a","make":"Hyundai","model":"Atos","features":"","colour":"white","engine":"1100cc","ordering":"2","published":"1","image":"images\/vehicles\/atos.jpg"}];

Which looks right so I don't know what is causing the error. Any ideas? Cheers.

Safari points to an error here in the code safari error

philip
  • 403
  • 1
  • 4
  • 14
  • Share the code from where error is coming – Niklesh Raut May 23 '16 at 15:32
  • Please take care when formatting your questions. I've attempted to format the PHP code for you, although it's not event close to syntactically correct as I assume you've copied it from a logger of some sort. – Rory McCrossan May 23 '16 at 15:35
  • I hit send thinking it would preview the code. my err and deserve the negative scores ! as you can tell from the code, i'm a complete novice. – philip May 23 '16 at 15:40

3 Answers3

27

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.

YakovL
  • 7,557
  • 12
  • 62
  • 102
Niklesh Raut
  • 34,013
  • 16
  • 75
  • 109
  • 1
    Hey Rishi, kudos for spotting that. Thanks a million. the annoying thing is that the error console in safari (as shown in my edited original post) points to the json_encode line. I don't know if I can, but will try and update the title so as it will help others. – philip May 23 '16 at 16:03
  • 2
    Now that you've told me what the error is, there are so many topics regarding this. In my defence Safari console did not point to the right area of code at fault. Will rename it with the safar error message so as it may help others – philip May 23 '16 at 16:09
  • Thanks a million! Everything was working fine until I tried it on an iPhone... +! – J. Allan Oct 29 '16 at 20:22
  • For what it's worth, this seems to be fixed in newer versions of iOS Safari. – jayp Jan 14 '17 at 03:44
  • @jayp: May be it is because safari new version is not available in windows, and people still using old safari version. – Niklesh Raut Nov 14 '19 at 11:20
8

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' };
}
7

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.

Josh Wood
  • 461
  • 3
  • 14
  • Thanks for explaining your debugging process for this kind of an error – it's nice to gain some wisdom from the experience of others, if not myself. I'll try to keep this methodology in mind for next time I have such an error... Was facing the same error myself for a few hours (due to going on tangents trying to convert to pure ES5 with Babel, then reading up on modules in JavaScript, having issues with configuration, and coming back to this error again when it seemed like it just wasn't necessary to transpile to ES5 because browsers have supported ES6 for a long time)... Bc we used Stage 3... – user3773048 Jun 07 '20 at 17:39