{"id":2031,"date":"2025-02-25T22:36:27","date_gmt":"2025-02-25T14:36:27","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=2031"},"modified":"2025-02-25T22:36:27","modified_gmt":"2025-02-25T14:36:27","slug":"javascript-%e6%a8%a1%e6%9d%bf%e5%ad%97%e7%ac%a6%e4%b8%b2","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/02\/25\/javascript-%e6%a8%a1%e6%9d%bf%e5%ad%97%e7%ac%a6%e4%b8%b2\/","title":{"rendered":"JavaScript \u6a21\u677f\u5b57\u7b26\u4e32"},"content":{"rendered":"\n<p><strong>JavaScript \u6a21\u677f\u5b57\u7b26\u4e32\uff08Template Strings\uff09<\/strong> \u662f\u4e00\u79cd\u65b0\u7684\u5b57\u7b26\u4e32\u8868\u793a\u65b9\u6cd5\uff0c\u5b83\u5141\u8bb8\u5728\u5b57\u7b26\u4e32\u4e2d\u5d4c\u5165\u8868\u8fbe\u5f0f\uff0c\u5e76\u4e14\u652f\u6301\u591a\u884c\u5b57\u7b26\u4e32\uff0c\u4f7f\u7528\u53cd\u5f15\u53f7\uff08<code>`<\/code>\uff09\u5305\u56f4\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1. <strong>\u521b\u5efa\u6a21\u677f\u5b57\u7b26\u4e32<\/strong><\/h3>\n\n\n\n<p>\u6a21\u677f\u5b57\u7b26\u4e32\u901a\u8fc7\u53cd\u5f15\u53f7\uff08<code>`<\/code>\uff09\u6765\u5b9a\u4e49\uff0c\u800c\u4e0d\u662f\u5355\u5f15\u53f7\uff08<code>'<\/code>\uff09\u6216\u53cc\u5f15\u53f7\uff08<code>\"<\/code>\uff09\u3002\u5b83\u53ef\u4ee5\u5305\u542b\u53d8\u91cf\u548c\u8868\u8fbe\u5f0f\uff0c\u4e5f\u53ef\u4ee5\u81ea\u52a8\u5904\u7406\u591a\u884c\u6587\u672c\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>let name = \"Alice\";\nlet greeting = `Hello, ${name}!`;  \/\/ \u4f7f\u7528 `${}` \u5d4c\u5165\u8868\u8fbe\u5f0f\nconsole.log(greeting);  \/\/ \u8f93\u51fa: Hello, Alice!<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">2. <strong>\u63d2\u5165\u53d8\u91cf\u6216\u8868\u8fbe\u5f0f<\/strong><\/h3>\n\n\n\n<p>\u6a21\u677f\u5b57\u7b26\u4e32\u5141\u8bb8\u5728\u5b57\u7b26\u4e32\u4e2d\u5d4c\u5165\u53d8\u91cf\u3001\u8868\u8fbe\u5f0f\u3001\u51fd\u6570\u8c03\u7528\u7b49\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>let name = \"Bob\";\nlet age = 25;\nlet message = `${name} is ${age} years old.`;  \/\/ \u63d2\u5165\u53d8\u91cf\nconsole.log(message);  \/\/ \u8f93\u51fa: Bob is 25 years old.\n\nlet sum = 10 + 5;\nlet result = `10 + 5 = ${sum}`;  \/\/ \u63d2\u5165\u8868\u8fbe\u5f0f\nconsole.log(result);  \/\/ \u8f93\u51fa: 10 + 5 = 15<\/code><\/pre>\n\n\n\n<p>\u6a21\u677f\u5b57\u7b26\u4e32\u4e2d\u7684 <code>${}<\/code> \u7528\u6765\u5305\u542b\u8868\u8fbe\u5f0f\uff0c\u53ef\u4ee5\u6267\u884c\u66f4\u590d\u6742\u7684\u64cd\u4f5c\uff0c\u5982\u51fd\u6570\u8c03\u7528\u6216\u8fd0\u7b97\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>let a = 5;\nlet b = 3;\nlet result = `The sum of ${a} and ${b} is ${a + b}.`;\nconsole.log(result);  \/\/ \u8f93\u51fa: The sum of 5 and 3 is 8.<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">3. <strong>\u591a\u884c\u5b57\u7b26\u4e32<\/strong><\/h3>\n\n\n\n<p>\u6a21\u677f\u5b57\u7b26\u4e32\u652f\u6301\u591a\u884c\u6587\u672c\uff0c\u4e0d\u9700\u8981\u4f7f\u7528\u6362\u884c\u7b26\uff08<code>\\n<\/code>\uff09\u6216\u62fc\u63a5\u7b26\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>let multiLineString = `This is a string\nthat spans across\nmultiple lines.`;\nconsole.log(multiLineString);\n\/\/ \u8f93\u51fa:\n\/\/ This is a string\n\/\/ that spans across\n\/\/ multiple lines.<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">4. <strong>\u6807\u7b7e\u6a21\u677f\u5b57\u7b26\u4e32\uff08Tagged Template Literals\uff09<\/strong><\/h3>\n\n\n\n<p>\u6807\u7b7e\u6a21\u677f\u5b57\u7b26\u4e32\u5141\u8bb8\u4f60\u5728\u6a21\u677f\u5b57\u7b26\u4e32\u524d\u9762\u52a0\u4e0a\u4e00\u4e2a\u51fd\u6570\uff08\u6807\u7b7e\uff09\uff0c\u8fd9\u4e2a\u51fd\u6570\u53ef\u4ee5\u5bf9\u6a21\u677f\u5b57\u7b26\u4e32\u8fdb\u884c\u81ea\u5b9a\u4e49\u5904\u7406\u3002\u901a\u8fc7\u6807\u7b7e\u51fd\u6570\uff0c\u4f60\u53ef\u4ee5\u8bbf\u95ee\u6a21\u677f\u5b57\u7b26\u4e32\u7684\u5185\u5bb9\uff0c\u5e76\u5bf9\u5176\u8fdb\u884c\u4fee\u6539\u6216\u5904\u7406\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>function tag(strings, ...values) {\n  let result = strings&#91;0];\n  values.forEach((value, index) =&gt; {\n    result += value.toUpperCase() + strings&#91;index + 1];\n  });\n  return result;\n}\n\nlet name = \"Alice\";\nlet age = 25;\nlet message = tag`My name is ${name} and I am ${age} years old.`;\nconsole.log(message);  \/\/ \u8f93\u51fa: My name is ALICE and I am 25 years old.<\/code><\/pre>\n\n\n\n<p>\u6807\u7b7e\u51fd\u6570\u63a5\u6536\u6a21\u677f\u5b57\u7b26\u4e32\u7684\u6587\u5b57\u90e8\u5206\u548c\u8868\u8fbe\u5f0f\u90e8\u5206\uff0c\u901a\u8fc7\u51fd\u6570\u5185\u90e8\u7684\u64cd\u4f5c\u53ef\u4ee5\u8fd4\u56de\u5904\u7406\u540e\u7684\u5b57\u7b26\u4e32\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">5. <strong>\u5d4c\u5957\u6a21\u677f\u5b57\u7b26\u4e32<\/strong><\/h3>\n\n\n\n<p>\u6a21\u677f\u5b57\u7b26\u4e32\u8fd8\u652f\u6301\u5d4c\u5957\u3002\u4f60\u53ef\u4ee5\u5728\u6a21\u677f\u5b57\u7b26\u4e32\u4e2d\u4f7f\u7528\u53e6\u4e00\u4e2a\u6a21\u677f\u5b57\u7b26\u4e32\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>let inner = `world`;\nlet outer = `Hello, ${inner}!`;\nconsole.log(outer);  \/\/ \u8f93\u51fa: Hello, world!<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u603b\u7ed3<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u6a21\u677f\u5b57\u7b26\u4e32<\/strong>\u901a\u8fc7\u53cd\u5f15\u53f7\uff08<code>`<\/code>\uff09\u5b9a\u4e49\uff0c\u5141\u8bb8\u5728\u5b57\u7b26\u4e32\u4e2d\u63d2\u5165\u53d8\u91cf\u3001\u8868\u8fbe\u5f0f\u7b49\u3002<\/li>\n\n\n\n<li><strong>\u63d2\u503c\u8868\u8fbe\u5f0f<\/strong>\u901a\u8fc7 <code>${}<\/code> \u63d2\u5165\u53d8\u91cf\u6216\u8ba1\u7b97\u7ed3\u679c\u3002<\/li>\n\n\n\n<li>\u652f\u6301<strong>\u591a\u884c\u5b57\u7b26\u4e32<\/strong>\uff0c\u65e0\u9700\u4f7f\u7528\u6362\u884c\u7b26\u3002<\/li>\n\n\n\n<li><strong>\u6807\u7b7e\u6a21\u677f\u5b57\u7b26\u4e32<\/strong>\u53ef\u4ee5\u5728\u6a21\u677f\u5b57\u7b26\u4e32\u524d\u52a0\u4e0a\u6807\u7b7e\u51fd\u6570\u8fdb\u884c\u5b9a\u5236\u5316\u5904\u7406\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u6a21\u677f\u5b57\u7b26\u4e32\u4f7f\u5f97\u5b57\u7b26\u4e32\u64cd\u4f5c\u66f4\u52a0\u7075\u6d3b\u3001\u7b80\u6d01\u3002\u66f4\u591a\u8be6\u7ec6\u5185\u5bb9\u8bf7\u5173\u5176\u4ed6\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>JavaScript \u6a21\u677f\u5b57\u7b26\u4e32\uff08Template Strings\uff09 \u662f\u4e00\u79cd\u65b0\u7684\u5b57\u7b26\u4e32\u8868\u793a\u65b9\u6cd5\uff0c\u5b83\u5141\u8bb8\u5728\u5b57\u7b26\u4e32 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[46],"tags":[],"class_list":["post-2031","post","type-post","status-publish","format-standard","hentry","category-javascript"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2031","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=2031"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2031\/revisions"}],"predecessor-version":[{"id":2032,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2031\/revisions\/2032"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=2031"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=2031"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=2031"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}