简体   繁体   中英

Javascript strict mode and versions

I want to use let in my code. Like so:

"use strict";

var b = 5;
for(let i =0;b > i; i++){
    alert(i);
}

This is working in Chrome and IE. But not in Firefox.

Wrapping my code in script tags with the type attribute set to "application/javascript;version=1.7" like so:

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

This fixes the problem in Firefox, but breaks the code in Chrome and IE. The error Firefox gives me when i execute the first code snippet:

SyntaxError: let is a reserved identifier

Is there any way to support all browsers?

Avoid using let unless you're using a transcoder/transpiler to convert your js to code that is currently widely supported.

Let became standard in ECMA-262 but it'll be several years before a large enough portion of visitors support it natively to use it.

The following have basic support for let , everything below will break:

  1. Chrome 41+
  2. Gecko 2.0
  3. IE11
  4. Opera 17+
  5. Safari ??

The above browsers likely have inconsistent implementations at this point, so it's best to avoid it.

Check out babel to transform.

Let as defined in ES2015 (ES6) is not yet supported in Firefox. It has an old version of let that is non standard and works slightly differently.

The current way to support it in all browsers is to use a transpiler like BabelJS .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM