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

4

2 回答 2

11

根据MDN 链接,(靠近底部),Safari 尚不支持此功能。

于 2016-01-29T02:18:51.007 回答
0

您需要在没有箭头功能的情况下重写它。

Afaik,除了语法之外的唯一区别是箭头函数保留了上下文。因此,您需要编写额外的一行来绑定上下文并将其与其他参数一起传递。

(不支持箭头功能还不错,这样就不会忘记没有新技术的第三世界。)

这更可重用:

array = [2,4,6]
function square(array) {
    array.map(function(item) {console.log(item*item)} )
}
square(array)
于 2020-09-22T10:22:20.313 回答