JS模板編譯的實(shí)現(xiàn)詳情
前言
編譯是一種格式變成另一種格式的過程。編譯會(huì)導(dǎo)致好的結(jié)果,比如書寫簡(jiǎn)單的代碼,編譯出來復(fù)雜的代碼;或者提高代碼的使用性能。
這里只聊聊模板編譯。
模板編譯的簡(jiǎn)單實(shí)現(xiàn)
寫一個(gè)最簡(jiǎn)單的模板
<p>Hello, {{name}}!</p>
這個(gè)模板用數(shù)據(jù){name: "world"}
渲染后的結(jié)果是:
<p>Hello, world!</p>
解決方法:最簡(jiǎn)單的方案,正則替換就行了
const compile = function(template, data) { return template.replace(/{{(.+?)}}/g, (match, key) => data[key]) } const template = "<p>Hello, I'm {{name}}! {{age}} years old!</p>" const data = { name: "hayes", age: 18 } const result = compile(template, data) console.log(result); // <p>Hello, I'm hayes! 18 years old!</p>
缺點(diǎn)很明顯,除了正則替換字段,其他啥都干不了,
來看看簡(jiǎn)單的嵌套需求:
模板:
<p>Hello, I'm {{user.name}}! {{user.age}} years old!</p>
渲染數(shù)據(jù);
const data = { user: { name: "hayes", age: 18 } }
現(xiàn)在再使用上面的方法,就失效了。還用正則的話,會(huì)很難做。因?yàn)樾枰稣Z法/詞法分析,來看看大括號(hào)內(nèi)寫的是什么了。
模板編譯
其實(shí)對(duì)于上述的模板,也可以使用如下方式來寫:
const compile = function(data) { return `<p>Hello, I'm ${data.name}! ${data.age} years old!</p>` }
好處:只需一次編譯,之后再使用就只需要直接填充數(shù)據(jù)即可。而且也方便支持data.user.name
這種形式。
工具:使用new Function
生成函數(shù)
生成一個(gè)函數(shù),傳入x
和 y
,執(zhí)行return x + y
來獲得求和的功能
const fn = new Function("x", "y", "return x + y");
打印fn
,可以看到輸出的內(nèi)容如下:
? anonymous(x,y) { return x + y }
1、構(gòu)建模板生成函數(shù)
傳入模板字符串,通過new Function
方式返回一個(gè)新函數(shù)。新函數(shù)接收一個(gè)obj
對(duì)象
const compile = function(template) { // 模板字符串 let result = ""; // ... return new Function("obj", result); }
2、正則替換
把{{xxx}}
找出來,替換為obj.xxx
const compile2 = function(template) { // 模板字符串 let result = template.replace(/{{(.+?)}}/g, (match, key) => { return `obj.${key}` }); result = `return "${result}"`; return new Function("obj", result); } const template2 = "<p>Hello, I'm {{user.name}}! {{user.age}} years old!</p>" const render2 = compile2(template2) console.log(render2);
此時(shí),函數(shù)打印如下:
? anonymous(obj ) { return "<p>Hello, I'm obj.user.name! obj.user.age years old!</p>" }
我們需要把字符串中的obj.user.name
和obj.user.age
變成動(dòng)態(tài)的。
修改一下正則
const compile2 = function(template) { // 模板字符串 let result = template.replace(/{{(.+?)}}/g, (match, key) => { return `" + obj.${key} + "` // 前后添上加號(hào) }); result = `return "${result}"`; return new Function("obj", result); } const template2 = "<p>Hello, I'm {{user.name}}! {{user.age}} years old!</p>" const render2 = compile2(template2) console.log(render2);
再來看看函數(shù)的打?。?/strong>
? anonymous(obj ) { return "<p>Hello, I'm " + obj.user.name + "! " + obj.user.age + " years old!</p>" }
最終代碼:
const compile = function(template) { // 模板字符串 let result = template.replace(/{{(.+?)}}/g, (match, key) => { return `" + obj.${key} + "` }); result = `return "${result}"`; return new Function("obj", result); } const template = "<p>Hello, I'm {{user.name}}! {{user.age}} years old!</p>" const render = compile(template) const data = { user: { name: "hayes", age: 18 } } const result = render(data) console.log(result); // <p>Hello, I'm hayes! 18 years old!</p>
渲染結(jié)果:
"<p>Hello, I'm hayes! 18 years old!</p>"
到此這篇關(guān)于JS模板編譯的實(shí)現(xiàn)詳情的文章就介紹到這了,更多相關(guān)JS模板編譯 內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
js實(shí)現(xiàn)仿百度風(fēng)云榜可重復(fù)多次調(diào)用的TAB切換選項(xiàng)卡效果
這篇文章主要介紹了js實(shí)現(xiàn)仿百度風(fēng)云榜可重復(fù)多次調(diào)用的TAB切換選項(xiàng)卡效果,涉及javascript鼠標(biāo)事件及頁面元素遍歷調(diào)用的實(shí)現(xiàn)技巧,非常簡(jiǎn)單實(shí)用,需要的朋友可以參考下2015-08-08js鍵盤方向鍵 文章翻頁跳轉(zhuǎn)的效果[小說站常用已支持firefox]
一些小說或圖片類網(wǎng)站,為了方便大家閱讀,往往會(huì)加入利用鍵盤方向鍵進(jìn)行翻頁、返回上一級(jí)、返回目錄、回首頁等功能。2009-05-05javascript 靜態(tài)對(duì)象和構(gòu)造函數(shù)的使用和公私問題
靜態(tài)對(duì)象和構(gòu)造函數(shù)的使用區(qū)別 平常我們會(huì)經(jīng)常使用JSON形式,或者var obj=function(){}亦或function(){}這么幾種對(duì)象的構(gòu)建辦法,有時(shí)會(huì)認(rèn)為這是等價(jià)的辦法,然而他們還有不同。2010-03-03uniapp調(diào)用百度語音實(shí)現(xiàn)錄音轉(zhuǎn)文字功能
這篇文章主要介紹了uniapp通過調(diào)用百度語音,實(shí)現(xiàn)錄音轉(zhuǎn)文字的功能。文中的示例代碼對(duì)我們學(xué)習(xí)或工作有一定的幫助,感興趣的小伙伴可以跟隨小編學(xué)習(xí)一下2021-12-12詳解V8是如何執(zhí)行一段JavaScript代碼原理
這篇文章主要為大家介紹了詳解V8是如何執(zhí)行一段JavaScript代碼原理詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-04-04基于微信小程序?qū)崿F(xiàn)人臉數(shù)量檢測(cè)的開發(fā)步驟
最近項(xiàng)目需求是統(tǒng)計(jì)當(dāng)前攝像頭中的人臉個(gè)數(shù),所以下面這篇文章主要給大家介紹了關(guān)于基于微信小程序?qū)崿F(xiàn)人臉數(shù)量檢測(cè)的相關(guān)資料,文中通過圖文介紹的非常詳細(xì),需要的朋友可以參考下2022-12-12移動(dòng)端H5喚起APP的寫法實(shí)例(IOS、android)
最近在做掃碼之后的h5頁面喚醒App的功能,所以記錄一下,這篇文章主要給大家介紹了關(guān)于移動(dòng)端H5喚起APP的相關(guān)資料,需要的朋友可以參考下2021-07-07