Convert a string to a template string

前端 未结 19 2404
轮回少年
轮回少年 2020-11-22 08:30

Is it possible to create a template string as a usual string

let a=\"b:${b}\";

an then convert it into a template string

le         


        
19条回答
  •  滥情空心
    2020-11-22 09:10

    You can use the string prototype, for example

    String.prototype.toTemplate=function(){
        return eval('`'+this+'`');
    }
    //...
    var a="b:${b}";
    var b=10;
    console.log(a.toTemplate());//b:10
    

    But the answer of the original question is no way.

提交回复
热议问题