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

发布于 2024-08-23 04:18:36 字数 636 浏览 8 评论 0原文

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

我想尝试的功能之一是 JavaScript 的 let 关键字

我似乎无法让他们的任何“let”示例在 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 ;执行 let foo = "bar" 时的 before 语句

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

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.

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

I can't seem to get any of their 'let' examples to work in Firefox 3.6 (User-Agent string: 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 statement when executing let foo = "bar".

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

<逆流佳人身旁 2024-08-30 04:18:36

编辑:所有现代浏览器都支持 letconst,并且是 ECMAScript 2015 (ES6) 规范。

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

IE11 上,当 letfor 循环一起使用时,变量未绑定到 for正如您所期望的那样,它的行为就像 var 所做的那样...

另请参阅:let< /a> 和 const 支持。


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

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

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

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

Basically if you don't need to support anything below IE11, let and const are safe to use nowadays.

On IE11 there's a small quirk with let when used with for loops, the variable is not bound to the for block as you would expect, it behaves as var did...

See also: let and const support.


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

On browser environments you should include the JavaScript version number in your script tag to use it:

<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>
紫罗兰の梦幻 2024-08-30 04:18:36

截至 2017 年 4 月:

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

  • iOS Safari 在 OS 10 之前不支持 let(例如,OS 9 不支持)。

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

     for (let i = 0; i < 3; ++i) {
          设置超时(函数(){
              控制台.log(i);
           }, i * 100);
      }
     

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

As of April 2017:

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

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

  • Some older browsers, such as IE9-IE11, support an early version of let but don't support the semantics defined by ES2015 (particularly in relation to declarations in the headers of for loops). 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.

近箐 2024-08-30 04:18:36

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

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

送君千里 2024-08-30 04:18:36

Internet Explorer 和 Opera 不支持 let在任何浏览器版本上,Firefox 从 2.0 版开始,Safari 从 3.2 版开始。

请参阅 Wikipedia 上的 JavaScript 版本表

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

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

Internet Explorer and Opera don't support let on any browser version, Firefox since version 2.0 and Safari since 3.2.

See this JavaScript version table on Wikipedia.

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

<script type="application/javascript;version=1.7"> ... </script>
↘人皮目录ツ 2024-08-30 04:18:36

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

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/

落在眉间の轻吻 2024-08-30 04:18:36

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文