当前位置:网站首页>深入浅出node模板解析错误escape is not a function
深入浅出node模板解析错误escape is not a function
2022-07-06 04:06:00 【xzlAwin】
深入浅出node模板解析错误escape is not a function
操作
var escape = function (html) {
return String(html)
.replace(/&(?!\w+;)/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/"/g, '"')
}
var complie = function (str) {
console.log(str)
var tpl = str.replace(/<%=([\s\S]+?)%>/g, function (match, code) {
// 转义
return "' + escape(" + code + ") + '"
}).replace(/<%-([\s\S]+?)%>/g, function (match, code) {
// 正常输出
return "' + " + code + " + '"
}).replace(/<%([\s\S]+?)%>/g, function (match, code) {
return "';\n" + code + "\n tpl += '"
}).replace(/\'\n/g, '\'')
.replace(/\n\'/gm, '\'')
console.log('----------代码替换模板----------')
console.log(tpl)
tpl = "tpl = '" + tpl + "'"
tpl = tpl.replace(/''/g, '\'\\n\'')
tpl = ' var tpl = ""\n with (obj || {}) {\n ' + tpl + '\n }\n return tpl'
return new Function('obj', 'escape', tpl)
}
var render = function(complie, data) {
console.log('----------构造函数----------')
console.log(complie.toString())
console.log('----------运行函数----------')
return complie(data)
}
var tpl = [
'<% if (obj.user) { %>',
'<h2><%=user.name%></h2>',
'<% } else { %>',
'<h2>匿名用户</h2>',
'<% } %>'].join('\n')
console.log('----------模板----------')
console.log(tpl)
console.log(render(complie(tpl), {user: {name: 'Jackson Tian'}}))
//console.log('\n\n')
//console.log(render(complie(tpl), {}))
异常信息
TypeError: escape is not a function
at eval (eval at complie (F:\workspace\javascript workspace\plNode\prj8_5_4_2\src\template4.js:27:10), <anonymous>:7:21)
at render (F:\workspace\javascript workspace\plNode\prj8_5_4_2\src\template4.js:34:9)
at Object.<anonymous> (F:\workspace\javascript workspace\plNode\prj8_5_4_2\src\template4.js:49:13)
at Module._compile (internal/modules/cjs/loader.js:1085:14)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10)
at Module.load (internal/modules/cjs/loader.js:950:32)
at Function.Module._load (internal/modules/cjs/loader.js:790:12)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:76:12)
at internal/main/run_main_module.js:17:47
分析原因
- escape不是函数,因为 return new Function(‘obj’, ‘escape’, tpl) 语句中 ‘escape’是函数,complie(data, escape) 调用的时候需要传递 escape函数,否则找不到escape函数
解决方法
方法一
- 调用时,传递escape函数
complie(data, escape)
方法二
- 自己调用自己时,传递escape函数
complie.call(complie, data, escape)
# 或者
complie.call(this, data, escape)
完整代码
var escape = function (html) {
return String(html)
.replace(/&(?!\w+;)/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/"/g, '"')
}
var complie = function (str) {
console.log(str)
var tpl = str.replace(/<%=([\s\S]+?)%>/g, function (match, code) {
// 转义
return "' + escape(" + code + ") + '"
}).replace(/<%-([\s\S]+?)%>/g, function (match, code) {
// 正常输出
return "' + " + code + " + '"
}).replace(/<%([\s\S]+?)%>/g, function (match, code) {
return "';\n" + code + "\n tpl += '"
}).replace(/\'\n/g, '\'')
.replace(/\n\'/gm, '\'')
console.log('----------代码替换模板----------')
console.log(tpl)
tpl = "tpl = '" + tpl + "'"
tpl = tpl.replace(/''/g, '\'\\n\'')
tpl = ' var tpl = ""\n with (obj || {}) {\n ' + tpl + '\n }\n return tpl'
return new Function('obj', 'escape', tpl)
}
var render = function(complie, data) {
console.log('----------构造函数----------')
console.log(complie.toString())
console.log('----------运行函数----------')
return complie(data, escape)
// 等价于
//return complie.call(this, data, escape)
}
var tpl = [
'<% if (obj.user) { %>',
'<h2><%=user.name%></h2>',
'<% } else { %>',
'<h2>匿名用户</h2>',
'<% } %>'].join('\n')
console.log('----------模板----------')
console.log(tpl)
console.log(render(complie(tpl), {user: {name: 'Jackson Tian'}}))
//console.log('\n\n')
//console.log(render(complie(tpl), {}))
边栏推荐
- Benefits of automated testing
- How many of the 10 most common examples of istio traffic management do you know?
- Ybtoj coloring plan [tree chain dissection, segment tree, tarjan]
- 在 .NET 6 中使用 Startup.cs 更简洁的方法
- Align items and align content in flex layout
- Plus d'un milliard d'utilisateurs de grandes entreprises comme Facebook ont été compromis, il est temps de se concentrer sur le did
- math_极限&微分&导数&微商/对数函数的导函数推导(导数定义极限法)/指数函数求导公式推导(反函数求导法则/对数求导法)
- KS003基于JSP和Servlet实现的商城系统
- How to standardize the deployment of automated testing?
- 判断当天是当月的第几周
猜你喜欢
【可调延时网络】基于FPGA的可调延时网络系统verilog开发
MySQL master-slave replication
10 exemples les plus courants de gestion du trafic istio, que savez - vous?
[practice] mathematics in lottery
Database, relational database and NoSQL non relational database
自动化测试的好处
[FPGA tutorial case 11] design and implementation of divider based on vivado core
math_极限&微分&导数&微商/对数函数的导函数推导(导数定义极限法)/指数函数求导公式推导(反函数求导法则/对数求导法)
JVM的手术刀式剖析——一文带你窥探JVM的秘密
【leetcode】1189. Maximum number of "balloons"
随机推荐
Global and Chinese market of rubber wheel wedges 2022-2028: Research Report on technology, participants, trends, market size and share
Global and Chinese markets for fire resistant conveyor belts 2022-2028: Research Report on technology, participants, trends, market size and share
MySql數據庫root賬戶無法遠程登陸解决辦法
20、 EEPROM memory (AT24C02) (similar to AD)
Facebook等大厂超十亿用户数据遭泄露,早该关注DID了
Path of class file generated by idea compiling JSP page
Custom event of C (31)
Prime Protocol宣布在Moonbeam上的跨链互连应用程序
C (thirty) C combobox listview TreeView
【FPGA教程案例12】基于vivado核的复数乘法器设计与实现
阿里测试师用UI自动化测试实现元素定位
Thread sleep, thread sleep application scenarios
[FPGA tutorial case 12] design and implementation of complex multiplier based on vivado core
Redis (replicate dictionary server) cache
asp. Core is compatible with both JWT authentication and cookies authentication
Global and Chinese markets for medical gas manifolds 2022-2028: Research Report on technology, participants, trends, market size and share
Align items and align content in flex layout
【按键消抖】基于FPGA的按键消抖模块开发
mysql关于自增长增长问题
DM8 archive log file manual switching