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 '>'
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 '>'
- 2 "As I know, Safari has ok support for ES6" - why do you think so? – Bergi Commented Jan 29, 2016 at 2:07
- hm... i thought Safari and Chrome tend to be very similar... or maybe they are similar only for the layout engine? – nonopolarity Commented Jan 29, 2016 at 2: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 – dsgriffin Commented Jan 29, 2016 at 2:17
- @daniel - that was my edit, and realized that was also the answer shortly after :) – Krease Commented Jan 29, 2016 at 2: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 Commented Jan 29, 2016 at 2:30
2 Answers
Reset to default 11Based on the MDN link, (near the bottom), Safari does not yet support this feature.
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)
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1743647453a4483998.html
评论列表(0条)