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

前端 未结 1 1334
情书的邮戳
情书的邮戳 2021-01-15 04:33

I have been using string literals in my javascript. I would like an error message to be shown if string literals are not supported. caniuse

My Idea was that i would

相关标签:
1条回答
  • 2021-01-15 05:11

    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
    
    0 讨论(0)
提交回复
热议问题