JS编程实现简单模板引擎变量替换
程序员文章站
2022-05-26 09:28:19
...
render (str) {
return function (obj) {
str = str.replace('year', obj.year)
str = str.replace('(month)', obj.month)
str = str.replace('$(day)', obj.day)
return str
}
},
tranform () {
const year = '2018'
const month = '9'
const day = '21'
const str = this.render('year-(month)-$(day)')({ year, month, day })
console.log(str)
}
转载于:https://www.jianshu.com/p/8cc2267616eb