7

For some reason, ES6 code that runs well in the current Chrome or Firefox cannot run in Safari - for example, arrow functions. As I know, Safari has ok support for ES6. Is there something that needs to be done?

Example:

var arr = [1,3,5].map((i) => i*i);
console.log(arr);

Or if it is a full .html file:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title></title>
    </head>
    <body>

        <script>
            "use strict";

            var arr = [1,3,5].map((i) => i*i);
            console.log(arr);

        </script>
    </body>
</html>

Safari (I am using 9.0.3) keeps on giving SyntaxError: Unexpected token '>'

nonopolarity
  • 146,324
  • 131
  • 460
  • 740
  • 2
    "*As I know, Safari has ok support for ES6*" - why do you think so? – Bergi Jan 29 '16 at 02:07
  • hm... i thought Safari and Chrome tend to be very similar... or maybe they are similar only for the layout engine? – nonopolarity Jan 29 '16 at 02:09
  • 1
    Chrome uses Blink whereas Safari uses Webkit still I believe. At the bottom of the mdn article you linked it states no support for Safari - you should look into [babel](https://babeljs.io/) – dsgriffin Jan 29 '16 at 02:17
  • @daniel - that was my edit, and realized that was also the answer shortly after :) – Krease Jan 29 '16 at 02:21
  • @daniel yeah, Chris linked that in... I thought for something so basic, Safari might have it... I think I was also trying something very basic, such as `let` and block scope or something else, and Safari gave me an error, so I thought there must be something I did wrong, such as not enabling ES6 support – nonopolarity Jan 29 '16 at 02:30
  • http://caniuse.com/#feat=arrow-functions – Mouloud85 Mar 22 '16 at 13:15
  • Safari is YEARLY released and super slow when it comes to implementing the new web features like Internationalization, Pointer Events, Web Components, CSS Variables, Service Workers, or ASM.js optimizations. Hopefully it would not become the next IE. – LeOn - Han Li Jul 04 '16 at 03:19

2 Answers2

11

Based on the MDN link, (near the bottom), Safari does not yet support this feature.

Krease
  • 15,805
  • 8
  • 54
  • 86
  • 3
    ok, i re-checked... Safari 9 supports some ES6, but for "Arrow function", the support right now is 0: http://kangax.github.io/compat-table/es6/ – nonopolarity Jan 29 '16 at 02:32
0

You need to rewrite it without arrow-functions.

Afaik, the only difference apart from the syntax is that arrow-functions keep the context. So you need to write an extra line to bind the context and pass it along the other parameters.

(It's kind of nice to not support arrow-functions, so that the third world that doesn't have new technology isn't forgotten.)

This is more reuseable:

array = [2,4,6]
function square(array) {
    array.map(function(item) {console.log(item*item)} )
}
square(array)
agiopnl
  • 1,190
  • 9
  • 18