0

I am using BackboneJS in my app. Below code works on google chrome but in IE (version 10) it gives syntax error in function parameter:

Syntax Error: Expected ')'

Below is my code:

initialize: function (options='default value') {
    console.log(options) 
},

I have handled it by using if-else condition inside the function for default parameter, but I am unable to understand the reason that why it is working in chrome but not in IE 10 ?

T J
  • 42,762
  • 13
  • 83
  • 138
Haziq
  • 2,048
  • 1
  • 16
  • 27
  • 1
    *"Though this syntax is for Ecmascript 5"* - it is not. It's ES6, and IE 10 doesn't support it – T J Oct 30 '17 at 10:25
  • thanks for pointing out but ES6 is supported to some extent. accepted answer describes in details what is supported and whats not. – Haziq Oct 30 '17 at 10:37
  • 2
    IE 10 is release in September 4, 2012. ES6 = ES2015. It's not supported. The accepted answer is not using ES6. It is not practical to check for ES6 support and write 2 variants of code. You should use a compiler like babel if you want to write ES6 code and support for older browsers. – T J Oct 30 '17 at 12:38
  • @TJ the code was not intended to be ES6. I've written the snippet using the older style in order to support the browser, but I've included the links for OP to look into regarding ES6. – mutantkeyboard Oct 30 '17 at 14:51

1 Answers1

0

It doesn't work like that in IE. Refer to this, and for checking the ES6 compatibility check this

So given the above results, you could do the check for you old code, and then for unsupported browsers write something that goes like this:

var MyPerson = Backbone.Model.extend({
  defaults : {
    fname : "John",
    lname : "Smith",
    totalSales : "0"
  },
  initialize: function(options) {
    options = options || "default options";
    console.log('A model instance named: ' + this.get("fname") +  ' ' + this.get("lname") + ' was created.');
  }
});

But the same logic applies to views as well.

mutantkeyboard
  • 1,614
  • 1
  • 16
  • 44
  • 1
    As [TJ stated in the comments](https://stackoverflow.com/questions/47010148/backbonejs-why-internet-explorer-does-not-accept-the-default-parameter-given-to#comment80977229_47010148), don't write 2 variants of your code just to use ES6 features. Use ES5 only, or compile your ES6 to ES5 using something like Babel. – Emile Bergeron Oct 30 '17 at 14:34