最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

firefox - Can't use "let" keyword in Safari Javascript? - Stack Overflow

programmeradmin1浏览0评论

I don't understand the best way to use "let" keyword...

  • In IE11 and Chrome45 I can use it fine
  • In Safari8.0.4, like in older versions of Chrome, it gives the error "unexpected use of reserved word 'let'"
  • In Firefox the let keyword only works inside <script type="application/javascript;version=1.7"/>, but this script type isn't even recognized as Javascript in IE11, Chrome45, Safari8.

Here's a JSFiddle that shows it in action: /

So -- I don't mind requiring users to use modern versions of their browsers.

And I don't mind excluding Safari if there honestly is no version of Safari that supports this keyword. (Is that really true? Why does everyone spend all their time griping about IE when Safari seems so much worse in ES6 terms? Have I missed something?).

But how should I allow "let" to work in Firefox while not preventing Chrome/IE? (I haven't yet found links from people griping about how Firefox script tag behaves differently from Chrome, and I'd have expected more complaints, so I figure I must have missed something obvious...)

I don't understand the best way to use "let" keyword...

  • In IE11 and Chrome45 I can use it fine
  • In Safari8.0.4, like in older versions of Chrome, it gives the error "unexpected use of reserved word 'let'"
  • In Firefox the let keyword only works inside <script type="application/javascript;version=1.7"/>, but this script type isn't even recognized as Javascript in IE11, Chrome45, Safari8.

Here's a JSFiddle that shows it in action: https://jsfiddle.net/p6cgtyg6/1/

So -- I don't mind requiring users to use modern versions of their browsers.

And I don't mind excluding Safari if there honestly is no version of Safari that supports this keyword. (Is that really true? Why does everyone spend all their time griping about IE when Safari seems so much worse in ES6 terms? Have I missed something?).

But how should I allow "let" to work in Firefox while not preventing Chrome/IE? (I haven't yet found links from people griping about how Firefox script tag behaves differently from Chrome, and I'd have expected more complaints, so I figure I must have missed something obvious...)

Share Improve this question edited Mar 23, 2015 at 0:42 Knu 15.1k6 gold badges59 silver badges92 bronze badges asked Mar 22, 2015 at 11:53 Lucian WischikLucian Wischik 2,3361 gold badge23 silver badges27 bronze badges 9
  • 1 Why is lack of support for let an issue? ECMA-262 6th Edition is still only a draft, many of its features are not supported by all browsers. There are some ES5 features that are not supported yet. – RobG Commented Mar 22, 2015 at 12:08
  • 1 "for (var i=0; i<c.length; i++) { $("<span/>").text(c[i].title).click(function() { DoStuff(c[i]); }).appendTo(p); }" -- this code will behave wrongly. The easiest and cleanest codefix is to use "let" inside the for loop. I don't really want to write yuckier code now when ES6 is shortly around the corner and is mostly supported ad will solve the problem nicely... – Lucian Wischik Commented Mar 23, 2015 at 14:11
  • @LucianWischik no need for let in that case, though. many easier ways rather than adding a dependency on a 6=>5 compiler. – Factor Mystic Commented Mar 23, 2015 at 17:48
  • Restricting accessibility to your web site to a couple of the most recent browser versions because you want to use a new feature of a draft specification is your prerogative of course. But your predicament is entirely self imposed. ;-) – RobG Commented Mar 24, 2015 at 2:52
  • @RobG it's an issue because time marches on, ES6 is now standard, and safari is the new IE. so the question still remains and is no longer self-imposed. – worc Commented Sep 29, 2016 at 0:13
 |  Show 4 more comments

3 Answers 3

Reset to default 7

Concerning Safari 8, it's just not supported ; for that browser, I'd recommend using Babel.

If you have the gut feeling that this bug won't be fixed anytime soon* then you could have a script that detect Firefox which would then inject your script(s) with the appropriate value for the type attribute.

As a side note, I would advise not to use let blocks—unless you wanna use this transpiler—nor let expressions which will be dropped.

* fixed in Firefox 44

let is a part of ECMAScript 6 specification, and ECMAScript 6 itself is in 'draft' status. Even in its incomplete form its features aren't supported fully by actual browser versions.

Since you want to dive into ES6 today for production, your best bet is to use ES6 to ES5 transpiler, most prominent ones are Babel and Traceur, which are available as both CLI and packages for the build system of your choice. There are online tools for Babel and Traceur to try them out. And Scratch JS extension for Chrome uses both Babel and Traceur and is excellent for developing and compiling ES6 scripts if the build system is not an option.

Here is up-to-date comparison table that embraces ES6 feature support in both browsers and ES6 compilers.

And here is a great collection of ES6-related tools.

See browser compatability of this ES6 keyword.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/let#Browser_compatibility

Also see this SO post for ES6 feature detection.

发布评论

评论列表(0)

  1. 暂无评论