js中${key}的写法比较

String.prototype.format = function () { var str = this for (var i = 0; i < arguments.length; i++) { var re = new RegExp('\\{' + i + '\\}', 'gm') str = str.replace(re, arguments[i]) } return str }

调用:
var str = "我叫{0},我有{1}元" str.format("Aton",100)//"我叫Aton,我有100元"

js中${key}的写法比较
文章图片
仿写.png 【js中${key}的写法比较】第二种:
var name = "Aton" var money = 100 console.log(`我叫${name},我有${money}元`) //我叫Aton,我有100元

    推荐阅读