creating an error message if browser does not support ES6 Template Literals

点点圈 提交于 2019-12-02 09:41:41

Yes. This is one of the legitimate uses of eval:

var supportsTemplateLiterals = false;
try {
    eval("`foo`");
    supportsTemplateLiterals = true;
}
catch (e) {
}
console.log("Supports template literals? " + supportsTemplateLiterals);

It works because the main code parses on a pre-ES2015 JavaScript engine, but the code in the eval doesn't; parsing chokes on the template literal.

On Chrome, Firefox, Edge, etc, that shows

Supports template literals? true

On IE (any version), it shows:

Supports template literals? false
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!