目前哪些浏览器支持 JavaScript 的“let”关键字?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2356830/
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-08-22 23:55:03  来源:igfitidea点击:

What browsers currently support JavaScript's 'let' keyword?

javascriptfirefoxfirefox3.6

提问by David Murdoch

I'm developing an app and don't have to ever worry about Internet Explorer and was looking into some of the features present in A+ grade browsers that aren't in Internet Explorer1.

我正在开发一个应用程序,不必担心 Internet Explorer,并且正在研究 Internet Explorer1 中没有的 A+ 级浏览器中存在的一些功能。

One of these features I wanted to play around with is JavaScript's let keyword

我想使用的这些特性之一是JavaScript 的 let 关键字

I can't seem to get any of their 'let' examples to work in Firefox 3.6 (User-Agentstring: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2) Gecko/20100115 Firefox/3.6 (.NET CLR 3.5.30729)). I get SyntaxError: missing ; before statementwhen executing let foo = "bar".

我似乎无法让他们的任何“让”示例在 Firefox 3.6 中工作(用户代理字符串:Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2) Gecko/20100115 Firefox/3.6 (.NET CLR 3.5.30729))。我SyntaxError: missing ; before statement在执行时得到let foo = "bar".

So, what browsers support the let keyword? (Or am I doing something wrong?)

那么,哪些浏览器支持 let 关键字?(还是我做错了什么?)

采纳答案by CMS

EDIT: letand constare supported by all modern browsers and are part of the ECMAScript 2015 (ES6)specification.

编辑letconst由所有现代浏览器都支持,并且是一部分的ECMAScript 2015年(ES6)规范。

Basically if you don't need to support anything below IE11, letand constare safe to use nowadays.

基本上,如果您不需要支持 IE11 以下的任何内容,let并且const现在可以安全使用。

On IE11there's a small quirk with letwhen used with forloops, the variable is not bound to the forblock as you would expect, it behaves as vardid...

IE11 上有一个小怪癖,let当与for循环一起使用时,变量没有for像你期望的那样绑定到块,它的行为就像var......

See also: letand constsupport.

另见:letconst支持。



Old and outdated answer from 2010:Those extensions are not ECMA-Standard, they are supported only by the Mozilla implementation.

2010 年旧的和过时的答案:这些扩展不是 ECMA 标准,它们仅由 Mozilla 实现支持。

On browser environments you should include the JavaScript version numberin your scripttag to use it:

在浏览器环境中,您应该在标签中包含 JavaScript版本号script以使用它:

<script type="application/javascript;version=1.7">  
  var x = 5;
  var y = 0;

  let (x = x+10, y = 12) {
    alert(x+y + "\n");
  }

  alert((x + y) + "\n");
</script>

回答by T.J. Crowder

As of April 2017:

截至 2017 年 4 月:

  • All up-to-date major browsers such as Chrome, Firefox, and Edge support the ES2015 (aka "ES6") letkeyword.

  • iOS Safari did not support letuntil OS 10 (e.g, OS 9 did not).

  • Some older browsers, such as IE9-IE11, support an early version of letbut don'tsupport the semantics defined by ES2015 (particularly in relation to declarations in the headers of forloops). So it's not a syntax error, and it does declare the variable, but it doesn't work the way it's supposed to. For instance, in a correct implementation, the following logs 0, 1, and 2; on IE9-IE11, it logs 3, 3, 3:

     for (let i = 0; i < 3; ++i) {
          setTimeout(function() {
              console.log(i);
           }, i * 100);
      }
     

  • Obsolete browsers such as IE8 do not support it at all.

  • 所有最新的主流浏览器,如 Chrome、Firefox 和 Edge 都支持 ES2015(又名“ES6”)let关键字。

  • iOS Safarilet直到 OS 10才支持(例如,OS 9 不支持)。

  • 一些较旧的浏览器,例如 IE9-IE11,支持ES2015 定义的语义(特别是与循环标头中的声明有关)的早期版本,let支持for。所以这不是语法错误,它确实声明了变量,但它没有按照预期的方式工作。例如,在正确的实现中,以下日志为 0、1 和 2;在 IE9-IE11 上,它记录 3、3、3:

     for (let i = 0; i < 3; ++i) {
          setTimeout(function() {
              console.log(i);
           }, i * 100);
      }
     

  • IE8等过时的浏览器根本不支持它。

回答by sam

There is partial support in Internet Explorer 11 (forscope is incorrect) and full support in all current browsers (ECMAScript 6 compatibility table: let).

Internet Explorer 11 部分支持(for范围不正确),所有当前浏览器完全支持(ECMAScript 6 兼容性表:let)。

回答by Harmen

Internet Explorer and Operadon't support leton any browser version, Firefox since version 2.0 and Safari since 3.2.

Internet Explorer 和Opera不支持let任何浏览器版本,Firefox 自 2.0 版和 Safari 自 3.2 版起。

See this JavaScript version tableon Wikipedia.

请参阅维基百科上的此JavaScript 版本表

I just found outthat you need to define whether you use JavaScript 1.7 or not. So your code will be:

我刚刚发现您需要定义是否使用 JavaScript 1.7。所以你的代码将是:

<script type="application/javascript;version=1.7"> ... </script>

回答by jmoreno

Apparently Internet Explorer 10in Edgemode supports let, per JavaScript Version Information.

显然,根据JavaScript 版本信息Edge模式下的Internet Explorer 10支持。let

回答by pursang

A great deal of time has passed since this question was first asked: the 'let' and 'const' keywords have been introduced in ECMAScript 2015 (ES6). Search for 'let' or 'const' in this awesome ES6 compatibility table: https://kangax.github.io/compat-table/es6/

自从第一次提出这个问题以来,已经过去了很长时间:ECMAScript 2015 (ES6) 中引入了“let”和“const”关键字。在这个很棒的 ES6 兼容性表中搜索“let”或“const”:https://kangax.github.io/compat-table/es6/

回答by Matthew

Just an update: Chrome now supports letbut only if you declare the "use strict"; directive.

只是更新:Chrome 现在支持,let但前提是您声明"use strict"; 指示。