当前位置:网站首页>Explain in simple terms node template parsing error escape is not a function
Explain in simple terms node template parsing error escape is not a function
2022-07-06 04:08:00 【xzlAwin】
Explain profound theories in simple language node Template parsing error escape is not a function
operation
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) {
// escape
return "' + escape(" + code + ") + '"
}).replace(/<%-([\s\S]+?)%>/g, function (match, code) {
// Normal output
return "' + " + code + " + '"
}).replace(/<%([\s\S]+?)%>/g, function (match, code) {
return "';\n" + code + "\n tpl += '"
}).replace(/\'\n/g, '\'')
.replace(/\n\'/gm, '\'')
console.log('---------- Code replacement template ----------')
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('---------- Constructors ----------')
console.log(complie.toString())
console.log('---------- Operation function ----------')
return complie(data)
}
var tpl = [
'<% if (obj.user) { %>',
'<h2><%=user.name%></h2>',
'<% } else { %>',
'<h2> Anonymous users </h2>',
'<% } %>'].join('\n')
console.log('---------- Templates ----------')
console.log(tpl)
console.log(render(complie(tpl), {user: {name: 'Jackson Tian'}}))
//console.log('\n\n')
//console.log(render(complie(tpl), {}))
Abnormal information
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
The analysis reason
- escape It's not a function , because return new Function(‘obj’, ‘escape’, tpl) In the sentence ‘escape’ Is the function ,complie(data, escape) When calling, you need to pass escape function , Otherwise I can't find it escape function
resolvent
Method 1
- Invocation time , Pass on escape function
complie(data, escape)
Method 2
- Call yourself , Pass on escape function
complie.call(complie, data, escape)
# perhaps
complie.call(this, data, escape)
Complete code
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) {
// escape
return "' + escape(" + code + ") + '"
}).replace(/<%-([\s\S]+?)%>/g, function (match, code) {
// Normal output
return "' + " + code + " + '"
}).replace(/<%([\s\S]+?)%>/g, function (match, code) {
return "';\n" + code + "\n tpl += '"
}).replace(/\'\n/g, '\'')
.replace(/\n\'/gm, '\'')
console.log('---------- Code replacement template ----------')
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('---------- Constructors ----------')
console.log(complie.toString())
console.log('---------- Operation function ----------')
return complie(data, escape)
// Equivalent to
//return complie.call(this, data, escape)
}
var tpl = [
'<% if (obj.user) { %>',
'<h2><%=user.name%></h2>',
'<% } else { %>',
'<h2> Anonymous users </h2>',
'<% } %>'].join('\n')
console.log('---------- Templates ----------')
console.log(tpl)
console.log(render(complie(tpl), {user: {name: 'Jackson Tian'}}))
//console.log('\n\n')
//console.log(render(complie(tpl), {}))
边栏推荐
- Lora gateway Ethernet transmission
- User datagram protocol UDP
- 51nod 1130 n factorial length V2 (Stirling approximation)
- math_极限&微分&导数&微商/对数函数的导函数推导(导数定义极限法)/指数函数求导公式推导(反函数求导法则/对数求导法)
- Global and Chinese markets for patent hole oval devices 2022-2028: Research Report on technology, participants, trends, market size and share
- MySQL master-slave replication
- KS008基于SSM的新闻发布系统
- C (XXIX) C listbox CheckedListBox Imagelist
- Global and Chinese markets for fire resistant conveyor belts 2022-2028: Research Report on technology, participants, trends, market size and share
- Unity中几个重要类
猜你喜欢
![[FPGA tutorial case 11] design and implementation of divider based on vivado core](/img/39/f337510c2647d365603a8485583a20.png)
[FPGA tutorial case 11] design and implementation of divider based on vivado core

Database, relational database and NoSQL non relational database

简易博客系统

C#(二十八)之C#鼠标事件、键盘事件

Lora gateway Ethernet transmission

Basic knowledge of binary tree, BFC, DFS

Comprehensive ability evaluation system

Ks008 SSM based press release system
![[Key shake elimination] development of key shake elimination module based on FPGA](/img/47/c3833c077ad89d4906e425ced945bb.png)
[Key shake elimination] development of key shake elimination module based on FPGA

Plus d'un milliard d'utilisateurs de grandes entreprises comme Facebook ont été compromis, il est temps de se concentrer sur le did
随机推荐
使用JS完成一个LRU缓存
如何修改表中的字段约束条件(类型,default, null等)
Viewing and verifying backup sets using dmrman
DM8 backup set deletion
【leetcode】1189. Maximum number of "balloons"
Ipv4中的A 、B、C类网络及子网掩码
Unity中几个重要类
How to modify field constraints (type, default, null, etc.) in a table
2/10 parallel search set +bfs+dfs+ shortest path +spfa queue optimization
Global and Chinese market of plasma separator 2022-2028: Research Report on technology, participants, trends, market size and share
ESP32_ FreeRTOS_ Arduino_ 1_ Create task
asp. Core is compatible with both JWT authentication and cookies authentication
2/12 didn't learn anything
Conditionally [jsonignore]
Practical development of member management applet 06 introduction to life cycle function and user-defined method
Ks003 mall system based on JSP and Servlet
Cf464e the classic problem [shortest path, chairman tree]
Web components series (VII) -- life cycle of custom components
Cf603e pastoral oddities [CDQ divide and conquer, revocable and search set]
User datagram protocol UDP