{"id":10251,"date":"2021-12-29T23:17:20","date_gmt":"2021-12-29T15:17:20","guid":{"rendered":"https:\/\/www.zhangxinxu.com\/wordpress\/?p=10251"},"modified":"2021-12-30T01:39:55","modified_gmt":"2021-12-29T17:39:55","slug":"js-tagged-templates","status":"publish","type":"post","link":"https:\/\/www.zhangxinxu.com\/wordpress\/2021\/12\/js-tagged-templates\/","title":{"rendered":"JS \u6807\u7b7e\u6a21\u677f\uff08Tagged templates\uff09\u4ec0\u4e48\u65f6\u5019\u4f7f\u7528\uff1f"},"content":{"rendered":"<p>by <a href=\"https:\/\/www.zhangxinxu.com\/\">zhangxinxu<\/a> from <a href=\"https:\/\/www.zhangxinxu.com\/wordpress\/?p=10251\">https:\/\/www.zhangxinxu.com\/wordpress\/?p=10251<\/a> \u946b\u7a7a\u95f4-\u946b\u751f\u6d3b<br \/>\n\u672c\u6587\u6b22\u8fce\u5206\u4eab\u4e0e\u805a\u5408\uff0c\u5168\u6587\u8f6c\u8f7d\u5c31\u4e0d\u5fc5\u4e86\uff0c\u5c0a\u91cd\u7248\u6743\uff0c\u5708\u5b50\u5c31\u8fd9\u4e48\u5927\uff0c\u82e5\u6025\u7528\u53ef\u4ee5\u8054\u7cfb\u6388\u6743\u3002<\/p>\n<p><script>window.wxShareImgUrl = 'https:\/\/www.zhangxinxu.com\/study\/image\/share\/202112-tagged-template.jpg';<\/script><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202112\/tag-template-cover.png\" width=\"446\" height=\"288\" alt=\"\u5723\u8bde\u661f\u7a7a\u56fe\" class=\"alignright clip thumb size-medium\" \/><\/p>\n<h3>\u4e00\u3001\u5148\u4e86\u89e3\u4e0b\u4ec0\u4e48\u662f\u6807\u7b7e\u6a21\u677f<\/h3>\n<p>\u6807\u7b7e\u6a21\u677f\u662f\u6a21\u677f\u5b57\u7b26\u4e32\u4f7f\u7528\u7684\u4e00\u79cd\u9ad8\u7ea7\u5f62\u5f0f\uff0c\u7528\u4ee3\u7801\u793a\u610f\u5c31\u662f\u2014\u2014<\/p>\n<p>\u8fd9\u662f\u6211\u4eec\u5e38\u89c1\u7684\u6a21\u677f\u5b57\u7b26\u4e32\uff1a<\/p>\n<pre>const author = 'zhangxinxu';\r\nconsole.log(`write by ${author}`);<\/pre>\n<p>\u8fd9\u4e2a\u5c31\u662f\u6807\u7b7e\u6a21\u677f\uff0c\u662f\u6a21\u677f\u5b57\u7b26\u4e32\u4f7f\u7528\u7684\u9ad8\u7ea7\u5f62\u5f0f\uff1a<\/p>\n<pre>const author = 'zhangxinxu';\r\nfunction tag (arr, exp) {\r\n    return `${arr[0]}${exp}`;\r\n}\r\nconsole.log(tag`write by ${author}`);<\/pre>\n<p>\u4e24\u6bb5\u5185\u5bb9\u8fd4\u56de\u7684\u7ed3\u679c\u662f\u4e00\u6837\u7684\u3002<\/p>\n<p>\u4ed4\u7ec6\u770b <code>tag`write by ${author}`<\/code> \u8fd9\u6bb5\u4ee3\u7801\uff0c\u5176\u4e2d <code>tag<\/code> \u5c31\u662f\u6807\u7b7e\u6a21\u677f\u4e2d\u7684\u201c\u6807\u7b7e\u201d\uff0c<code>`write by ${author}`<\/code> \u5c31\u662f\u6807\u7b7e\u6a21\u677f\u4e2d\u7684\u201c\u6a21\u677f\u201d\u3002<\/p>\n<p>\u56e0\u6b64\uff0c\u201c\u6807\u7b7e\u201d\u5b9e\u9645\u4e0a\u5e76\u4e0d\u662f\u6307\u7684\u6807\u7b7e\uff0c\u800c\u662f\u7c7b\u4f3c\u4e8e\u6807\u7b7e\u6027\u8d28\u7684\u51fd\u6570\uff0c\u201c\u6a21\u677f\u201d\u5219\u662f\u8fd9\u4e2a\u51fd\u6570\u7684\u53c2\u6570\u3002<\/p>\n<h4>\u8bed\u6cd5<\/h4>\n<p>\u6807\u7b7e\u6a21\u677f\u7531\u6807\u7b7e\u51fd\u6570\u548c\u6a21\u677f\u5b57\u7b26\u4e32\u4e24\u90e8\u5206\u7ec4\u6210\uff0c\u5176\u4e2d\u6807\u7b7e\u51fd\u6570\u7684\u540d\u79f0\u5927\u5bb6\u53ef\u4ee5\u6839\u636e\u9879\u76ee\u89c4\u8303\u968f\u610f\u547d\u540d\uff0c\u6a21\u677f\u5b57\u7b26\u4e32\u5f80\u5f80\u662f\u662f\u9700\u8981\u5904\u7406\u7684\u6570\u636e\u5185\u5bb9\u3002<\/p>\n<p>\u5176\u4e2d\uff0c\u7406\u89e3\u7684\u96be\u70b9\u5728\u6807\u7b7e\u51fd\u6570\u4e0a\u3002<\/p>\n<p>\u53c2\u6570\u662f\u8fd9\u6837\u7684\uff08\u5047\u8bbe\u51fd\u6570\u540d\u662f <code>tag<\/code>\uff09\uff1a<\/p>\n<pre>tag(arrStrings, exp1, exp2, exp3, ...)<\/pre>\n<p>\u5176\u4e2d\uff0carrStrings \u6307\u7684\u662f\u88ab <code>${...}<\/code> \u8fd9\u79cd\u8868\u8fbe\u5f0f\u5206\u9694\u7684\u5b57\u7b26\u4e32\uff0c<code>exp1, exp2, ...<\/code> \u5206\u522b\u8868\u793a\u7b2c1\u4e2a <code>${...}<\/code> \u5360\u4f4d\u7b26\u4e2d\u8868\u8fbe\u5f0f\u7684\u503c\uff0c\u7b2c2\u4e2a <code>${...}<\/code> \u8868\u8fbe\u5f0f\u7684\u503c&#8230;<\/p>\n<p>\u4f8b\u5982\uff1a<\/p>\n<pre>tag`write by ${author}, welcome to share!`<\/pre>\n<p>\u6b64\u65f6\uff0c<code>arrStrings<\/code> \u5c31\u662f <code>['write by ', ', welcome to share!']<\/code>\uff0c\u7531\u4e8e\u53ea\u6709\u4e00\u4e2a <code>${...}<\/code> \u5360\u4f4d\u7b26\uff0c\u56e0\u6b64\uff0c<code>exp1<\/code> \u5c31\u662f <code>author<\/code> \u5bf9\u5e94\u7684\u53d8\u91cf\u503c\uff0c<code>exp2<\/code> \u6ca1\u6709\u5bf9\u5e94\u7684\u503c\uff0c\u56e0\u6b64\u662f <code>undefined<\/code>\u3002<\/p>\n<p>\u6211\u4eec\u53ef\u4ee5\u6d4b\u8bd5\u4e0b\uff1a<\/p>\n<pre>const author = 'zhangxinxu';\r\nfunction tag (arr, exp1, exp2) {\r\n    console.log(arr[0], arr[1], exp1, exp2);\r\n}\r\ntag`write by ${author}, welcome to share!`<\/pre>\n<p>\u8f93\u51fa\u7684\u7ed3\u679c\u662f\uff1a<\/p>\n<pre>\/\/ write by\r\n\/\/ , welcome to share!\r\n\/\/ zhangxinxu\r\n\/\/ undefined<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202112\/2021-12-27_183900.png\" width=\"385\" height=\"194\" alt=\"\"><\/p>\n<p>\u597d\uff0c\u73b0\u5728\u6211\u4eec\u5df2\u7ecf\u77e5\u9053\u6807\u7b7e\u6a21\u677f\u662f\u4e2a\u4ec0\u4e48\u4e1c\u897f\u4e86\uff0c\u5173\u952e\u95ee\u9898\u662f\uff0c\u8fd9\u4e2a\u770b\u8d77\u6765\u6709\u4e9b\u5389\u5bb3\u7684\u7528\u6cd5\u6709\u4ec0\u4e48\u7528\u5462\uff1f<\/p>\n<p>\u4ece\u4e0a\u9762\u7684\u6848\u4f8b\u6765\u770b\uff0c\u76f4\u63a5\u6a21\u677f\u5b57\u7b26\u4e32\u4e00\u628a\u68ad\u4e0d\u66f4\u597d\u3002<\/p>\n<p>\u5173\u4e8e\u8fd9\u4e2a\u95ee\u9898\uff0c\u6211\u662f\u8fd9\u4e48\u8ba4\u4e3a\u7684\u3002<\/p>\n<p><small class=\"hidden\">\/\/zxx: \u5982\u679c\u4f60\u770b\u5230\u8fd9\u6bb5\u6587\u5b57\uff0c\u8bf4\u660e\u4f60\u73b0\u5728\u8bbf\u95ee\u662f\u4e0d\u662f\u539f\u6587\u7ad9\u70b9\uff0c\u66f4\u597d\u7684\u9605\u8bfb\u4f53\u9a8c\u5728\u8fd9\u91cc\uff1ahttps:\/\/www.zhangxinxu.com\/wordpress\/?p=10251\uff08\u4f5c\u8005\u5f20\u946b\u65ed\uff09<\/small><\/p>\n<h3>\u4e8c\u3001\u6807\u7b7e\u6a21\u677f\u4ec0\u4e48\u65f6\u5019\u4f7f\u7528\uff1f<\/h3>\n<p>\u8bb2\u8bb2\u6211\u7c97\u6d45\u7684\u770b\u6cd5\uff0c\u5982\u6709\u4e0d\u5bf9\uff0c\u6b22\u8fce\u6307\u6b63\u3002<\/p>\n<p>\u6211\u8ba4\u4e3a\u6807\u7b7e\u6a21\u677f\u5c31\u662f\u53d8\u4f53\u7248\u672c\u7684 <code>replace()<\/code> \u66ff\u6362\u51fd\u6570\u3002<\/p>\n<p>\u4e3e\u4e2a\u4f8b\u5b50\uff0c\u8bf7\u770b\u4e0b\u9762\u8fd9\u6bb5\u4ee3\u7801\uff1a<\/p>\n<pre>'write by ${author}, welcome to share!'\r\n  .replace(\/([\\w\\W]+)\\$\\{(\\w+)\\}([\\w\\W]+)\/, function (matches, $1, $2, $3) {\r\n    console.log([matches, $1, $2, $3].join('\\n'));\r\n});<\/pre>\n<p>\u7ed3\u679c\u662f\uff1a<\/p>\n<pre>write by ${author}, welcome to share!\r\nwrite by\r\nauthor\r\n, welcome to share!<\/pre>\n<p>\u622a\u56fe\u793a\u610f\u8fd0\u884c\u6548\u679c\uff1a<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202112\/2021-12-27_191942.png\" width=\"558\" height=\"208\" alt=\"replace \u8fd0\u884c\u6548\u679c\"><\/p>\n<p>\u6b64\u65f6\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u501f\u52a9 <code>$1<\/code>, <code>$2<\/code>, <code>$3<\/code> \u7b49\u53c2\u6570\u8fdb\u884c\u5339\u914d\u7684\u5185\u5bb9\u8fdb\u884c\u5904\u7406\u548c\u8fd4\u56de\uff0c\u4ece\u800c\u5f97\u5230\u6700\u7ec8\u7684\u66ff\u6362\u540e\u7684\u7ed3\u679c\u3002<\/p>\n<p>\u4f8b\u5982\uff1a<\/p>\n<pre>const author = 'zhangxinxu';\r\nfunction tag (str) {\r\n    return str.replace(\/([\\w\\W]+)\\$\\{(\\w+)\\}([\\w\\W]+)\/, function (matches, $1, $2, $3) {\r\n        return $1 + new Function('return ' + $2)() + $3;\r\n    });\r\n}\r\ntag('write by ${author}, welcome to share!');\r\n\/\/ \u7ed3\u679c\u662f write by zhangxinxu, welcome to share!<\/pre>\n<p>\u662f\u4e0d\u662f\u548c\u6807\u7b7e\u6a21\u677f\u7684\u5185\u6838\u5f88\u76f8\u4f3c\uff1f<\/p>\n<p>\u5b9e\u9645\u4e0a\uff0c\u5f88\u591a\u4f20\u7edf\u7684\u6a21\u677f\u5339\u914d\u5f15\u64ce\u5176\u5e95\u5c42\u5c31\u662f\u7c7b\u4f3c\u7684\u5b57\u7b26\u5339\u914d\u5904\u7406\u3002<\/p>\n<p>\u4e0b\u9762\u95ee\u9898\u6765\u4e86\uff0c\u65e2\u7136 <code>repalce()<\/code> \u66ff\u6362\u4e5f\u80fd\u5b9e\u73b0\u7c7b\u4f3c\u6807\u7b7e\u6a21\u677f\u7684\u6548\u679c\uff0c\u90a3\u4e3a\u4ec0\u4e48\u8fd8\u9700\u8981\u6807\u7b7e\u6a21\u677f\u5462\uff1f<\/p>\n<p>\u539f\u56e0\u6709\u4e8c\uff1a<\/p>\n<ol>\n<li>\u4f7f\u7528\u6210\u672c\u6bd4\u8f83\u9ad8\u7684\uff0c\u5199\u6b63\u5219\u8fd9\u79cd\u4e8b\u60c5\uff0c\u90a3\u4e0d\u662f\u4e00\u5929\u4e24\u5929\u53ef\u4ee5\u5b66\u4f1a\u7684\uff1b<\/li>\n<li>\u53ea\u80fd\u5904\u7406\u5b57\u7b26\u4e32\u7c7b\u578b\u7684\u53c2\u6570\uff0c\u9650\u5236\u4e86\u5176\u4f7f\u7528\u8303\u56f4\u3002<\/li>\n<\/ol>\n<p><code>repalce()<\/code> \u66ff\u6362\u8bed\u6cd5\u7684\u552f\u4e00\u4f18\u52bf\u5c31\u662f\u539f\u59cb\u7684\u66ff\u6362\u5185\u5bb9\u662f\u52a8\u6001\u7684\uff0c\u4e0d\u786e\u5b9a\u7684\uff0c\u5219\u975e\u5e38\u9002\u5408\uff0c\u5177\u6709\u4e0d\u53ef\u66ff\u4ee3\u6027\u3002<\/p>\n<p>\u4e8e\u662f\u56de\u5230\u4e86\u95ee\u9898\u672c\u8eab\uff0c\u4ec0\u4e48\u65f6\u5019\u9002\u5408\u4f7f\u7528\u6807\u7b7e\u6a21\u677f\uff1f<\/p>\n<p><strong>\u56de\u7b54\uff1a<\/strong><\/p>\n<p>\u9002\u5408\u7ed3\u6784\u5df2\u77e5\u7684\uff0c\u9700\u8981\u5bf9\u53d8\u91cf\u8fdb\u884c\u52a8\u6001\u5904\u7406\u7684\u573a\u666f\u3002<\/p>\n<p>\u6ce8\u610f\u8fd9\u91cc\u4e00\u4e2a\u5173\u952e\u70b9\uff0c\u9700\u8981\u5bf9\u53d8\u91cf\u52a8\u6001\u5904\u7406\uff0c\u5982\u679c\u53d8\u91cf\u53ea\u662f\u5355\u7eaf\u663e\u793a\uff08\u6216\u8005\u53ea\u662f\u7b80\u5355\u7684\u8868\u8fbe\u5f0f\u903b\u8f91\uff09\uff0c\u76f4\u63a5\u4f7f\u7528\u6a21\u677f\u5b57\u7b26\u4e32\u5c31\u597d\u4e86\uff0c\u53ef\u4ee5\u5b8c\u5168\u9a7e\u9a6d\uff0c\u8be6\u89c1\u201c<a href=\"https:\/\/www.zhangxinxu.com\/wordpress\/2020\/10\/es6-html-template-literal\/\">ES6\u6a21\u677f\u5b57\u7b26\u4e32\u5728HTML\u6a21\u677f\u6e32\u67d3\u4e2d\u7684\u5e94\u7528<\/a>\u201d\u8fd9\u7bc7\u6587\u7ae0\u3002<\/p>\n<p>\u4ee5\u53ca\uff0c\u5982\u679c\u53d8\u91cf\u662f\u6570\u503c\u3001\u51fd\u6570\u6216\u8005\u7eaf\u5bf9\u8c61\uff0c\u9700\u8981\u57fa\u4e8e\u7c7b\u578b\u505a\u52a8\u6001\u5904\u7406\uff0c\u5219\u4e5f\u975e\u5e38\u9002\u5408\u4f7f\u7528\u6807\u7b7e\u6a21\u677f\u3002<\/p>\n<h4>\u6848\u4f8b\u8bf4\u660e<\/h4>\n<p>\u4f8b\u5982\u4e0b\u9762\u4e00\u6bb5\u9080\u8bf7\u51fd\u6a21\u677f\uff1a<\/p>\n<blockquote><p>\u8bda\u631a\u9080\u8bf7 xxx \u5148\u751f\uff08\u5973\u58eb\uff09\u4f5c\u4e3a\u9009\u624b\uff08\u88c1\u5224\/\u8bb0\u5206\u5458\/\u6444\u5f71\u5e08\uff09\u4e8e xxxx\u5e74xx\u6708xx\u65e5\u53c2\u52a0\u4e0a\u6d77\u5f20\u6c5f\u676f\u5782\u9493\u7ade\u6280\u5927\u8d5b\u3002\u4e3b\u529e\u65b9\uff1a\u4e0a\u6d77\u5e02\u6d66\u4e1c\u9493\u9c7c\u534f\u4f1a<\/p><\/blockquote>\n<p>\u5176\u4e2d\uff0c\u52a8\u6001\u5185\u5bb9\u6709\u9080\u8bf7\u4eba\u540d\u5b57\uff0c\u6027\u522b\uff0c\u89d2\u8272\u4ee5\u53ca\u65e5\u671f\u3002<\/p>\n<p>\u7136\u800c\uff0c\u540e\u7aef\u8fd4\u56de\u7684\u6570\u636e\u662f\u8fd9\u6837\u7684\uff1a<\/p>\n<pre>{\r\n    \"name\": \"\u9093\u94c1\",\r\n    \"sex\": 0,\r\n    \"role\": 1,\r\n    \"time\": 1640678098887\r\n}<\/pre>\n<p>\u50cf\u6027\u522b\u548c\u89d2\u8272\u8fd4\u56de\u7684\u662fID\u6807\u8bc6\uff0c\u65e0\u6cd5\u76f4\u63a5\u586b\u8fdb\u53bb\uff0c\u9700\u8981\u52a8\u6001\u5904\u7406\u4e0b\uff0c\u800c\u4e14\u5904\u7406\u8d77\u6765\u5e76\u4e0d\u662f\u7b80\u5355\u7684\u8868\u8fbe\u5f0f\u5c31\u80fd\u641e\u5b9a\u7684\uff08\u6216\u8005\u8868\u8fbe\u5f0f\u4f1a\u6bd4\u8f83\u957f\uff09\uff0c\u6b64\u65f6\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u5728\u6807\u7b7e\u51fd\u6570\u4e2d\u5904\u7406\u5185\u5bb9\u8f6c\u6362\u7684\u95ee\u9898\uff0c\u76f8\u6bd4\u5728\u5916\u90e8\u5904\u7406\uff0c\u903b\u8f91\u4f1a\u66f4\u52a0\u5e72\u51c0\uff0c\u4ee3\u7801\u4f1a\u66f4\u52a0\u7b80\u6d01\u6613\u8bfb\u3002<\/p>\n<pre>const data = {\r\n    \"name\": \"\u9093\u94c1\",\r\n    \"sex\": 0,\r\n    \"role\": 1,\r\n    \"time\": 1640678098887\r\n}\r\nconst invite = function (arrs, nameExp, sexExp, roleExp, timeExp) {\r\n    let strName = nameExp;\r\n    \/\/ \u6027\u522b\u5904\u7406\r\n    let strSex = ['\u5148\u751f', '\u5973\u58eb'][sexExp];\r\n    \/\/ \u89d2\u8272\u5904\u7406\r\n    const role = {\r\n        \"1\": \"\u9009\u624b\",\r\n        \"2\": \"\u88c1\u5224\",\r\n        \"3\": \"\u8bb0\u5206\u5458\",\r\n        \"4\": \"\u6444\u5f71\u5e08\"\r\n    };\r\n    let strRole = role[roleExp];\r\n    \/\/ \u65e5\u671f\u5904\u7406\r\n    let strTime = new Date(timeExp).toLocaleDateString(undefined, {\r\n        year: 'numeric',\r\n        month: 'long',\r\n        day: 'numeric'\r\n    });\r\n\r\n    \/\/ \u8f93\u51fa\u5185\u5bb9\r\n    let output = [arrs[0]];\r\n\r\n    [strName, strSex, strRole, strTime].forEach((str, index) =&gt; {\r\n        output.push(str, arrs[index + 1] || '');\r\n    });\r\n\r\n    return output.join('');\r\n};\r\n\r\nlet content = invite`\u8bda\u631a\u9080\u8bf7${data.name}${data.sex}\u4f5c\u4e3a${data.role}\u4e8e${data.time}\u53c2\u52a0\u4e0a\u6d77\u5f20\u6c5f\u676f\u5782\u9493\u7ade\u6280\u5927\u8d5b\u3002\r\n\u4e3b\u529e\u65b9\uff1a\u4e0a\u6d77\u5e02\u6d66\u4e1c\u9493\u9c7c\u534f\u4f1a`;\r\n\r\nconsole.log(content);<\/pre>\n<p>\u6a21\u677f\u53ea\u8d1f\u8d23\u586b\u5145\u6570\u636e\uff0c\u81f3\u4e8e\u6700\u7ec8\u6570\u636e\u7684\u8fd4\u56de\uff0c\u7edf\u4e00\u5728\u6807\u7b7e\u51fd\u6570\u4e2d\u5904\u7406\uff0c\u6700\u540e\u7684\u6267\u884c\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n<blockquote><p>\u8bda\u631a\u9080\u8bf7\u9093\u94c1\u5148\u751f\u4f5c\u4e3a\u9009\u624b\u4e8e2021\u5e7412\u670828\u65e5\u53c2\u52a0\u4e0a\u6d77\u5f20\u6c5f\u676f\u5782\u9493\u7ade\u6280\u5927\u8d5b\u3002<br \/>\n\u4e3b\u529e\u65b9\uff1a\u4e0a\u6d77\u5e02\u6d66\u4e1c\u9493\u9c7c\u534f\u4f1a<\/p><\/blockquote>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202112\/2021-12-28_164138.png\" width=\"469\" height=\"112\" alt=\"\u5b57\u7b26\u8f93\u51fa\u6700\u7ec8\u7ed3\u679c\"><\/p>\n<h4>\u66f4\u590d\u6742\u7684\u6848\u4f8b<\/h4>\n<p>\u4e0a\u9762\u7684\u6848\u4f8b\u7684\u6570\u636e\u5904\u7406\u8fd8\u662f\u4e00\u5bf9\u4e00\u7684\u679a\u4e3e\u5904\u7406\uff0c\u590d\u7528\u6027\u5e76\u4e0d\u9ad8\u3002<\/p>\n<p>\u4e0b\u9762\u8fd9\u4e2a\u4f8b\u5b50\u5c31\u8981\u66f4\u590d\u6742\uff0c\u8981\u66f4\u62bd\u8c61\u4e00\u70b9\u3002<\/p>\n<p>\u5b9e\u73b0\u7684\u6548\u679c\u662f\uff0c\u4e00\u6bb5 HTML \u6807\u7b7e\u6a21\u677f\uff0c\u5982\u679c\u6709\u8bbe\u7f6e\u7c7b\u4f3c <code>onClick<\/code> \u8fd9\u6837\u7684 Function \u7c7b\u578b\u5360\u4f4d\uff0c\u5219\u6e32\u67d3\u51fa\u6765\u7684 DOM \u5143\u7d20\u81ea\u52a8\u7ed1\u5b9a\u8be5\u4e8b\u4ef6\u3002<\/p>\n<p>\u4f8b\u5982\uff1a<\/p>\n<pre>`&lt;button onClick=${() =&gt; addTodo()}&gt;\u6dfb\u52a0\u4efb\u52a1\u5217\u8868&lt;\/button&gt;`<\/pre>\n<p>\u4e0d\u4ec5\u6e32\u67d3\u6309\u94ae\u5143\u7d20\uff0c\u8fd8\u4f1a\u7ed9\u8fd9\u4e2a\u5143\u7d20\u7ed1\u5b9a <code>'click'<\/code> \u4e8b\u4ef6\u3002<\/p>\n<p>\u6709\u70b9\u7c7b\u4f3c\u4e8e JSX \u7684\u5b9e\u73b0\u3002<\/p>\n<p>\u5b8c\u6210\u4ee3\u7801\u793a\u610f\u5982\u4e0b\uff0cHTML\u90e8\u5206\uff1a<\/p>\n<pre>&lt;div id=\"app\"&gt;&lt;\/div&gt;<\/pre>\n<p>JS \u4ee3\u7801\u90e8\u5206\uff1a<\/p>\n<pre>&lt;script&gt;\r\nconst render = function (data, container) {\r\n    container.innerHTML = '';\r\n    container.append(element(data));\r\n};\r\nconst html = function (arr, ...keys) {\r\n    let result = [arr[0]];\r\n    keys.forEach(function(key, i) {\r\n      if (typeof key == 'function') {\r\n        result.push(i, arr[i + 1]);\r\n      } else {\r\n        result.push(key, arr[i + 1]);\r\n      }      \r\n    });\r\n    \/\/ \u521b\u5efa template \u5143\u7d20\r\n    let template = document.createElement('template');\r\n    template.innerHTML = result.join('');\r\n    \/\/ \u904d\u5386\u4e0e\u4e8b\u4ef6\u6dfb\u52a0\r\n    template.content.querySelectorAll('*').forEach(node =&gt; {\r\n        let attrs = node.attributes;\r\n        for (let i = attrs.length - 1; i &gt;= 0; i--) {\r\n            let attr = attrs[i].name;\r\n            let value = attrs[i].value;\r\n            if (\/^on[a-z]+$\/i.test(attr) &amp;&amp; !isNaN(parseFloat(value))) {           \r\n                node.removeAttribute(attr);\r\n                node.addEventListener(attr.replace(\/^on\/, ''), keys[Number(value)]);\r\n            }\r\n        }\r\n    });\r\n\r\n    return template.content;\r\n};\r\n\r\nlet todos = ['\u5403\u996d', '\u7761\u89c9', '\u6253\u8c46\u8c46'];\r\n\r\nfunction addTodo () {\r\n    todos.push(task.value);\r\n    render(todos, app);\r\n};\r\n\r\nlet element = function (todos) {\r\n    return html`&lt;h3&gt;\u4efb\u52a1\u5217\u8868(${todos.length})&lt;\/h3&gt;\r\n        &lt;ul&gt;\r\n        ${todos.map(\r\n            todo =&gt; `&lt;li&gt;${todo}&lt;\/li&gt;`\r\n        ).join('')}\r\n        &lt;\/ul&gt;\r\n        &lt;form onSubmit=\"${e =&gt; { e.preventDefault(); }}\"&gt;\r\n            &lt;input id=\"task\" required&gt;\r\n            &lt;button onClick=${() =&gt; addTodo()}&gt;\u6dfb\u52a0\u4efb\u52a1\u5217\u8868&lt;\/button&gt;\r\n        &lt;\/form&gt;\r\n    `;\r\n};\r\n    \r\nrender(todos, app);\r\n&lt;\/script&gt;<\/pre>\n<p>\u5b9e\u73b0\u7684\u539f\u7406\u5982\u4e0b\uff0c\u5982\u679c\u6a21\u677f\u53c2\u6570\u662f\u4e2a Function \u7c7b\u578b\uff0c\u5219\u628a\u8fd9\u4e2a Function \u66ff\u6362\u4e3a\u5bf9\u5e94\u7684\u7d22\u5f15\u503c\uff0c\u7136\u540e\u4f7f\u7528 <code>&lt;template&gt;<\/code> \u5143\u7d20\u6784\u9020 DOM \u7ed3\u6784\u7684\u65f6\u5019\uff0c\u5339\u914d\u5230\u7b26\u5408\u89c4\u5219\u7684 HTML \u5c5e\u6027\uff0c\u91cd\u65b0\u627e\u56de\u8be5 Function\uff0c\u4f7f\u7528 <code>addEventListener<\/code> \u6dfb\u52a0\u4e8b\u4ef6\u3002<\/p>\n<p>\u5e76\u8fd4\u56de\u5b8c\u6574\u7684\u6587\u6863\u7247\u6bb5\u3002<\/p>\n<p>\u6700\u7ec8\u5b9e\u73b0\u7684\u6548\u679c\u5982\u4e0b\uff0c\u9ed8\u8ba4\u8fdb\u5165\u9875\u9762\u53ef\u4ee5\u770b\u5230\u6e32\u67d3\u4e86 3 \u4e2a\u5217\u8868\uff1a<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202112\/2021-12-28_170208.png\" width=\"327\" height=\"196\" alt=\"\u9875\u9762\u5217\u8868\u6e32\u67d3\u793a\u610f\"><\/p>\n<p>\u7136\u540e\u6dfb\u52a0\u4e00\u4e2a\u9009\u9879\uff0c\u5e76\u70b9\u51fb\u6309\u94ae\uff0c\u4f1a\u770b\u5230\u65b0\u7684\u9009\u9879 append \u5230\u5217\u8868\u4e2d\u4e86\uff0c\u5982\u4e0b GIF \u52a8\u56fe\u6240\u793a\uff1a<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202112\/add-todo.gif\" width=\"369\" height=\"222\" alt=\"TODO\u5185\u5bb9\u6dfb\u52a0\u622a\u56fe\"><\/p>\n<p><span class=\"s\">\/\/zxx: \u8fd9\u4e2a\u4f8b\u5b50\u4e3b\u8981\u793a\u610f\u6e32\u67d3\uff0c\u5b9e\u9645\u4e0a\uff0c\u771f\u5b9e\u5f00\u53d1\u9700\u8981\u901a\u8fc7 DOM \u6bd4\u5bf9\u8fdb\u884c\u5185\u5bb9\u66f4\u65b0\uff0c\u800c\u4e0d\u662f\u50cf\u8fd9\u6837\u5168\u90e8\u66ff\u6362\u3002<\/span><\/p>\n<p>\u773c\u89c1\u4e3a\u5b9e\uff0c\u60a8\u53ef\u4ee5\u72e0\u72e0\u5730\u70b9\u51fb\u4f53\u9a8c\u6548\u679c\uff1a<a href=\"https:\/\/www.zhangxinxu.com\/study\/202112\/tagged-template-todo-list-demo.php\" rel=\"noopener\" target=\"_blank\">JS\u6807\u7b7e\u6a21\u677fHTML\u6e32\u67d3demo<\/a><\/p>\n<h4>\u5176\u4ed6\u6848\u4f8b<\/h4>\n<p>\u8fd9\u91cc\u7684\u6848\u4f8b\u6765\u81ea\u5fae\u535a\u7528\u6237 <a href=\"https:\/\/weibo.com\/u\/7578900612\" rel=\"noopener\" target=\"_blank\">\u7f16\u7a0b\u52a0<\/a> \u7684\u53cd\u9988\uff1a<\/p>\n<blockquote><p>\u5927\u90e8\u5206\u573a\u666f\u662f\u5b9e\u73b0 DSL\uff0c\u6bd4\u5982\u6587\u4e2d\u7684 html\uff0cstyled-components \u91cc\u7684 css\uff0c\u8fd8\u6709\u5404\u79cd sql\u3001graphql\u2026\u2026\u6bd4\u5982\u53ef\u4ee5\u5199\u4e00\u4e2a\u6bd4\u8f83\u4f18\u96c5\u7684\u3001\u7528\u6765\u5904\u7406 URL \u8f6c\u4e49\u7684 tag function\uff0c\u4f7f\u7528\u8005\u4e0d\u9700\u8981\u5173\u5fc3 URL \u8f6c\u4e49\u7684\u7ec6\u8282\uff1a<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202112\/2021-12-30_013558.png\" width=\"450\" height=\"328\" alt=\"URL \u5904\u7406\" class=\"alignnone size-medium\" \/>\n<\/p><\/blockquote>\n<h3>\u4e09\u3001\u7ed3\u8bed<\/h3>\n<p>\u603b\u7ed3\u4e00\u4e0b\uff0cJS \u6807\u7b7e\u6a21\u677f\u5e76\u4e0d\u662f\u4e00\u4e2a\u975e\u4f7f\u7528\u4e0d\u53ef\u7684\u7279\u6027\u3002<\/p>\n<p>\u5f53\u6a21\u677f\u7ed3\u6784\u56fa\u5b9a\uff0c\u540c\u65f6\u6570\u636e\u5904\u7406\u6bd4\u8f83\u590d\u6742\u7684\u65f6\u5019\uff0c\u4f1a\u6bd4\u8f83\u5408\u9002\u3002<\/p>\n<p>\u6216\u8005\uff0c\u4f60\u5e0c\u671b\u9690\u85cf\u5bf9\u4e0d\u540c\u6570\u636e\u5904\u7406\u7684\u7ec6\u8282\uff0c\u8ba9\u4ee3\u7801\u53d8\u5f97\u66f4\u5e72\u51c0\u3002<\/p>\n<p>\u6216\u8005\uff0c\u4f60\u5e0c\u671b\u5728\u56e2\u961f\u4ee3\u7801\u91cc\u9732\u4e24\u624b\uff0c\u90fd\u662f\u53ef\u4ee5\u4f7f\u7528\u7684\u3002<\/p>\n<p>OK\uff0c\u4ee5\u4e0a\u5c31\u662f\u672c\u6587\u7684\u5168\u90e8\u5185\u5bb9\u5566\uff0c\u5199\u5f97\u5306\u5306\u5fd9\u5fd9\u7684\uff0c\u6709\u9519\u8bef\u5728\u6240\u96be\u514d\uff0c\u6b22\u8fce\u6307\u6b63\uff0c\u4e5f\u6b22\u8fce\u70b9\u51fb\u8fd9\u91cc<button onclick=\"shareWeixin.click()\" class=\"entry-share-btn\">\u5206\u4eab<\/button>\u7ed9\u4f60\u7684\u5c0f\u4f19\u4f34\u4eec\u3002<\/p>\n<p>\u4e48\u4e48\u54d2\uff0c\u5143\u65e6\u5feb\u4e50\u3002<\/p>\n<p>\u5bf9\u4e86\uff0c\u524d\u4e24\u5929\u7f16\u8f91\u548c\u6211\u8bb2\uff0c\u6211\u7684\u65b0\u4e66\u300aCSS\u65b0\u4e16\u754c\u300b\u83b7\u5f97\u5e74\u5ea6\u7545\u9500\u65b0\u4e66\u5956\uff0c\u54c8\u54c8\uff0c\u6709\u4e9b\u610f\u5916\uff0c\u6bd5\u7adf 8 \u6708\u4e2d\u65ec\u624d\u4e0a\u67b6\uff0c\u542c\u5230\u8fd9\u4e2a\u6d88\u606f\u8fd8\u633a\u5f00\u5fc3\u7684\uff0c\u76ee\u524d<a href=\"https:\/\/weibo.com\/1263362863\/L80qC1Czu\" rel=\"noopener\" target=\"_blank\">\u5fae\u535a\u4e0a\u6709\u8fd9\u4e2a\u7b7e\u540d\u7248\u4e66\u7684\u62bd\u5956<\/a>\uff0c\u5927\u5bb6\u53ef\u4ee5\u8bd5\u8bd5\u8f6c\u53d1\u4e0b\uff0c\u5468\u4e94\u5f00\u5956\uff0c\u8bf4\u4e0d\u5b9a\u5c31\u662f\u4f60\u4e86\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202112\/my-award-s.jpg\" width=\"320\" height=\"361\" alt=\"\u5e74\u5ea6\u4f5c\u8005\u548c\u65b0\u4e66\u5956\" class=\"alignnone size-medium\" \/><\/p>\n<p>\u672c\u6587\u4e3a\u539f\u521b\u6587\u7ae0\uff0c\u6b22\u8fce\u5206\u4eab\uff0c\u52ff\u5168\u6587\u8f6c\u8f7d\uff0c\u5982\u679c\u5b9e\u5728\u559c\u6b22\uff0c\u53ef\u6536\u85cf\uff0c\u6c38\u4e0d\u8fc7\u671f\uff0c\u4e14\u4f1a\u53ca\u65f6\u66f4\u65b0\u77e5\u8bc6\u70b9\u53ca\u4fee\u6b63\u9519\u8bef\uff0c\u9605\u8bfb\u4f53\u9a8c\u4e5f\u66f4\u597d\u3002<br \/>\n\u672c\u6587\u5730\u5740\uff1a<a href=\"https:\/\/www.zhangxinxu.com\/wordpress\/?p=10251\">https:\/\/www.zhangxinxu.com\/wordpress\/?p=10251<\/a><\/p>\n<p>\uff08\u672c\u7bc7\u5b8c\uff09<\/p>\n","protected":false},"excerpt":{"rendered":"<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202112\/tag-template-cover.jpg\" width=\"160\" height=\"103\" alt=\"\u5723\u8bde\u661f\u7a7a\u56fe\" class=\"alignright size-medium\" \/><\/p>\n<p>JS \u6807\u7b7e\u6a21\u677f tagged templates \u662f\u4e00\u4e2a\u5f88\u6709\u610f\u601d\u7684\u8bed\u6cd5\u7279\u6027\uff0c\u662f\u6a21\u677f\u5b57\u7b26\u4e32\u7684\u9ad8\u7ea7\u5f62\u5f0f\uff0c\u90a3\u5e94\u8be5\u4ec0\u4e48\u65f6\u5019\u4f7f\u7528\u6807\u7b7e\u6a21\u677f\u5462\uff0c\u672c\u6587\u5c31\u6839\u636e\u6211\u4e2a\u4eba\u7684\u7406\u89e3\u7ed9\u5927\u5bb6\u505a\u4e0b\u89e3\u7b54\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1284],"tags":[1813,61,371,1811,928,1812,1683,1684],"_links":{"self":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/posts\/10251"}],"collection":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/comments?post=10251"}],"version-history":[{"count":0,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/posts\/10251\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/media?parent=10251"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/categories?post=10251"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/tags?post=10251"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}