无法在 Safari Javascript 中使用“let”关键字?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29194024/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 10:13:03  来源:igfitidea点击:

Can't use "let" keyword in Safari Javascript?

javascriptfirefoxsafariecmascript-6

提问by Lucian Wischik

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

我不明白使用“let”关键字的最佳方式...

  • 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.
  • 在 IE11 和 Chrome45 中我可以很好地使用它
  • 在 Safari8.0.4 中,就像在旧版本的 Chrome 中一样,它给出错误“意外使用保留字‘让’”
  • 在 Firefox 中, let 关键字只在 内有效<script type="application/javascript;version=1.7"/>,但这种脚本类型在 IE11、Chrome45、Safari8 中甚至不被识别为 Javascript。

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

这是一个 JSFiddle,显示它在行动: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?).

如果老实说没有支持此关键字的 Safari 版本,我不介意排除 Safari。(这是真的吗?当 Safari 在 ES6 术语中看起来更糟糕时,为什么每个人都花所有时间抱怨 IE?我错过了什么?)。

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...)

但是我应该如何允许“让”在 Firefox 中工作而不阻止 Chrome/IE?(我还没有找到抱怨 Firefox 脚本标签与 Chrome 的行为方式不同的人的链接,我预计会有更多的抱怨,所以我想我一定错过了一些明显的东西......)

采纳答案by Knu

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

关于 Safari 8,它只是不被支持;对于该浏览器,我建议使用Babel

If you have the gut feeling that this bugwon'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.

如果您有直觉认为此错误不会很快得到修复*,那么您可以拥有一个检测 Firefox 的脚本,然后该脚本将使用类型属性的适当值注入您的脚本。

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.

作为旁注,我建议不要使用 let 块——除非你想使用这个转译器——也不要使用 let将被删除的表达式。

* fixed in Firefox 44

* 已在 Firefox 44 中修复

回答by Estus Flask

letis 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.

let是 ECMAScript 6 规范的一部分,ECMAScript 6 本身处于“草案”状态。即使在其不完整的形式中,实际浏览器版本也不完全支持其功能。

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

由于您今天想深入研究 ES6 以进行生产,因此最好的选择是使用 ES6 到 ES5 转译器,最突出的是BabelTraceur,它们既可以作为 CLI 也可以作为您选择的构建系统的包使用。有可供BabelTraceur试用的在线工具。和划痕JSChrome扩展程序,同时使用巴贝尔和Traceur,是优秀的开发和编译器ES6脚本,如果构建系统是不是一种选择。

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

这是包含浏览器和 ES6 编译器中 ES6 功能支持的最新比较表

And here is a great collectionof ES6-related tools.

这里有大量与 ES6 相关的工具。

回答by click2install