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