【问题标题】:Template literals syntax is not working in IE11模板文字语法在 IE11 中不起作用
【发布时间】:2017-04-13 19:01:43
【问题描述】:

在其他浏览器(例如 Chrome)中使用“use strict”指令时,反引号字符在 IE11 中不被识别为有效字符。

考虑到即使在 Windows 10 用户中仍然广泛使用 IE11,对此行为有何解释?

        "use strict";

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

错误:{ "message": "无效字符", "文件名": "http://stacksnippets.net/js", “亚麻”:18, “科尔诺”:17 }

【问题讨论】:

  • 巴别塔呢?我认为在没有转译器的情况下使用 ES2015 仍然不安全。
  • @Belmin,我只是按照一个简单的教程,遇到了这个问题,但会考虑转译器
  • 为什么要关注 use strict 指令?你是说它在IE11草率模式下工作?
  • @Bergi,不确定您所说的草率模式是什么意思;它根本无法在 IE11 的边缘(默认)模式下工作。而关于“专注于 use strict 指令”,我只是在记录一个观察结果。
  • @usefulBee sloppy=non-strict。所以它在 IE11 中根本不起作用,因为它不支持(这个)ES6 功能,无论是否处于严格模式。

标签: javascript internet-explorer ecmascript-6 internet-explorer-11 template-literals


【解决方案1】:

如果您查看ECMAScript 6 compatibility table,您会发现 IE11 不支持模板文字。 "use strict"; 语句并没有真正改变任何东西,因为在确定代码是否处于严格模式之前,必须先对其进行解析,但无法解析,因为您使用的是解析器没有的语法不认识。

如果您希望您的代码在 IE11 中工作,您应该使用 Babel 进行转译。

【讨论】:

    【解决方案2】:

    如果您只想在现代 Javascript 中编写一个短代码 sn-p,例如 ES6,但需要在旧浏览器中工作的代码的 vanilla Javascript 版本,例如IE11,可以使用Babel REPL进行转译。

    例子:

    let person = {
      name: "John Doe",
      city: "Doeville"
    };
    
    const markup = `
     <div class="person">
        <h2>
            ${person.name}
        </h2>
        <p class="city">This person lives in ${person.city}.</p>
     </div>
    `;
    document.body.innerHTML = markup;

    被转译成:

    "use strict";
    
    var person = {
      name: "John Doe",
      city: "Doeville"
    };
    var markup = "\n <div class=\"person\">\n    <h2>\n        ".concat(person.name, "\n    </h2>\n    <p class=\"city\">This person lives in ").concat(person.city, ".</p>\n </div>\n");
    document.body.innerHTML = markup;

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-23
      • 2021-02-08
      • 1970-01-01
      • 1970-01-01
      • 2015-10-20
      • 1970-01-01
      相关资源
      最近更新 更多