Template literals not working in IE11 when “use st

2019-01-18 21:39发布

问题:

The back-tick character is not recognized as a Valid Character in IE11 when using the "use strict" directive while it works in other browsers, such as Chrome.

What is the interpretation of this behavior taking into consideration that IE11 is still widely used even among Windows 10 users??

        "use strict";

        function doIt() {
          let tt;
          tt = 50;
          alert(`${tt}`);
          alert("test");
        }
       doIt();

Error: { "message": "Invalid character", "filename": "http://stacksnippets.net/js", "lineno": 18, "colno": 17 }

回答1:

If you look at the ECMAScript 6 compatibility table, you'll see that template literals are not supported by IE11. The "use strict"; statement doesn't really change anything, because before it is determined whether a code is in strict mode, it has to be parsed first, but it can't be parsed, because you're using syntax that the parser doesn't recognize.

If you want your code to work in IE11, you should transpile it with Babel.