{"id":1925,"date":"2023-03-25T14:26:38","date_gmt":"2023-03-25T06:26:38","guid":{"rendered":""},"modified":"2023-03-25T14:26:38","modified_gmt":"2023-03-25T06:26:38","slug":"ES6 \u6a21\u677f\u6587\u5b57","status":"publish","type":"post","link":"https:\/\/bianchenghao6.com\/1925.html","title":{"rendered":"ES6 \u6a21\u677f\u6587\u5b57"},"content":{"rendered":"


\n <\/head>
\n <\/p>\n

\n

ES6 \u6a21\u677f\u6587\u5b57<\/h1>\n<\/p><\/div>\n

ES6 \u6a21\u677f\u5b57\u9762\u91cf<\/h2>\n
\n \u6a21\u677f\u5b57\u9762\u91cf\u662f ECMAScript 2015\/ES6 \u4e2d\u5f15\u5165\u7684\u4e00\u9879\u65b0\u529f\u80fd\u3002\u5b83\u63d0\u4f9b\u4e86\u4e00\u79cd\u521b\u5efa\u591a\u884c\u5b57\u7b26\u4e32\u548c\u6267\u884c\u5b57\u7b26\u4e32\u63d2\u503c\u7684\u7b80\u5355\u65b9\u6cd5\u3002\u6a21\u677f\u6587\u5b57\u662f\u5b57\u7b26\u4e32\u6587\u5b57\u5e76\u5141\u8bb8\u5d4c\u5165\u8868\u8fbe\u5f0f\u3002\n <\/div>\n
\n \u5728 ES6 \u4e4b\u524d\uff0c\u6a21\u677f\u6587\u5b57\u88ab\u79f0\u4e3a\u6a21\u677f\u5b57\u7b26\u4e32\u3002\u4e0e\u5b57\u7b26\u4e32\u4e2d\u7684\u5f15\u53f7\u4e0d\u540c\uff0c\u6a21\u677f\u6587\u5b57\u7531 \u53cd\u5f15\u53f7(` `) \u5b57\u7b26(QWERTY \u952e\u76d8\u4e2d ESC \u952e\u4e0b\u65b9\u7684\u952e)\u62ec\u8d77\u6765\u3002\u6a21\u677f\u6587\u5b57\u53ef\u4ee5\u5305\u542b\u5360\u4f4d\u7b26\uff0c\u7531\u7f8e\u5143\u7b26\u53f7\u548c\u5927\u62ec\u53f7($(expression}) \u8868\u793a\u3002\u5728\u53cd\u5f15\u53f7\u5185\uff0c\u5982\u679c\u6211\u4eec\u60f3\u4f7f\u7528\u4e00\u4e2a\u8868\u8fbe\u5f0f\uff0c\u90a3\u4e48\u6211\u4eec\u53ef\u4ee5\u5c06\u8be5\u8868\u8fbe\u5f0f\u653e\u5728($(expression}).\n <\/div>\n
\n \u8bed\u6cd5\n <\/div>\n
\n
var <\/span>str = `string value`;<\/pre>\n

<\/code>\n <\/div>\n

\u591a\u884c\u5b57\u7b26\u4e32<\/h2>\n
\n \u5728\u666e\u901a\u5b57\u7b26\u4e32\u4e2d\uff0c\u6211\u4eec\u5fc5\u987b\u4f7f\u7528\u8f6c\u4e49\u5e8f\u5217\\n \u6765\u4e3a\u521b\u5efa\u591a\u884c\u5b57\u7b26\u4e32\u63d0\u4f9b\u4e00\u4e2a\u65b0\u884c\u3002\u4f46\u662f\uff0c\u5728\u6a21\u677f\u5b57\u9762\u91cf\u4e2d\uff0c\u6ca1\u6709\u5fc5\u8981\u4f7f\u7528\\n \uff0c\u56e0\u4e3a\u5b57\u7b26\u4e32\u53ea\u6709\u5728\u83b7\u5f97\u53cd\u5f15\u53f7(`) \u5b57\u7b26\u65f6\u624d\u7ed3\u675f\u3002\n <\/div>\n
\n \u8ba9\u6211\u4eec\u8bd5\u7740\u7528\u4e0b\u9762\u7684\u4f8b\u5b50\u6765\u7406\u89e3\u5b83\u3002\n <\/div>\n
\n \u4f8b\u5b50\n <\/div>\n
\n
\/\/ Without template literal <\/span>\nconsole.log<\/span>('Without template literal \\n multiline string'<\/span>); \n  \n\/\/ With template literal <\/span>\nconsole.log<\/span>(`Using template literal\nmultiline string`);<\/pre>\n

<\/code>\n <\/div>\n

\n \u8f93\u51fa\n <\/div>\n
\n
Without template literal\n multiline string\nUsing template literal\nmultiline string<\/pre>\n

<\/code>\n <\/div>\n

\u5b57\u7b26\u4e32\u63d2\u503c<\/h2>\n
\n ES6 \u6a21\u677f\u6587\u5b57\u652f\u6301\u5b57\u7b26\u4e32\u63d2\u503c.\u6a21\u677f\u6587\u5b57\u53ef\u4ee5\u4f7f\u7528\u5360\u4f4d\u7b26\u8fdb\u884c\u5b57\u7b26\u4e32\u66ff\u6362\u3002\u8981\u4f7f\u7528\u666e\u901a\u5b57\u7b26\u4e32\u5d4c\u5165\u8868\u8fbe\u5f0f\uff0c\u6211\u4eec\u5fc5\u987b\u4f7f\u7528 ${} \u8bed\u6cd5\u3002\n <\/div>\n
\n \u793a\u4f8b-1\n <\/div>\n
\n
var <\/span>name = 'World'<\/span>;\nvar <\/span>cname = 'lidihuo'<\/span>;\nconsole.log<\/span>(`Hello, ${name}!\nWelcome to ${cname}`);<\/pre>\n

<\/code>\n <\/div>\n

\n \u8f93\u51fa\n <\/div>\n
\n
Hello, World!\nWelcome to lidihuo<\/pre>\n

<\/code>\n <\/div>\n

\n \u8ba9\u6211\u4eec\u770b\u53e6\u4e00\u4e2a\u5b57\u7b26\u4e32\u63d2\u503c\u7684\u4f8b\u5b50\u3002\n <\/div>\n
\n Example-2\n <\/div>\n
\n
var <\/span>x = 10;\nvar <\/span>y = 20;\nconsole.log<\/span>(`The product of the variables ${x} and ${y} is:\n ${x*y}`);<\/pre>\n

<\/code>\n <\/div>\n

\n \u8f93\u51fa\n <\/div>\n
\n
The product of the variables 10 and 20 is:\n200<\/pre>\n

<\/code>\n <\/div>\n

\u6807\u8bb0\u6a21\u677f<\/h2>\n
\n \u6807\u8bb0\u6a21\u677f\u662f\u6a21\u677f\u6587\u5b57\u7684\u66f4\u9ad8\u7ea7\u5f62\u5f0f\u4e4b\u4e00\u3002\u6807\u8bb0\u6a21\u677f\u6587\u5b57\u5141\u8bb8\u6211\u4eec\u4f7f\u7528\u51fd\u6570\u89e3\u6790\u6a21\u677f\u6587\u5b57.\n <\/div>\n
\n \u6807\u7b7e\u51fd\u6570\u7684\u7b2c\u4e00\u4e2a\u53c2\u6570\u5305\u542b\u4e00\u4e2a\u5177\u6709\u5b57\u7b26\u4e32\u503c\u7684\u6570\u7ec4\uff0c\u5176\u4f59\u53c2\u6570\u4e0e\u8868\u8fbe\u5f0f\u76f8\u5173\u3002\u6807\u8bb0\u6587\u5b57\u7684\u7f16\u5199\u4e0e\u51fd\u6570\u5b9a\u4e49\u7c7b\u4f3c\uff0c\u4f46\u4e0d\u540c\u4e4b\u5904\u5728\u4e8e\u6807\u8bb0\u6587\u5b57\u88ab\u8c03\u7528\u65f6\u3002\u8c03\u7528\u6587\u5b57\u4e0d\u9700\u8981\u62ec\u53f7()\u3002\n <\/div>\n
\n \u8ba9\u6211\u4eec\u770b\u770b\u6807\u8bb0\u6a21\u677f\u7684\u63d2\u56fe\u3002\n <\/div>\n
\n \u793a\u4f8b 1\n <\/div>\n
\n
function TaggedLiteral(str) { \n    console.log<\/span>(str); \n} \n  \nTaggedLiteral `Hello World`;<\/pre>\n

<\/code>\n <\/div>\n

\n \u8f93\u51fa\n <\/div>\n
\n
[ 'Hello World'<\/span> ]<\/pre>\n

<\/code>\n <\/div>\n

\n Example-2\n <\/div>\n
\n \u6211\u4eec\u8fd8\u53ef\u4ee5\u5728\u6807\u8bb0\u6587\u5b57\u4e2d\u4f20\u9012\u503c\u3002\u8be5\u503c\u53ef\u4ee5\u662f\u67d0\u4e2a\u8868\u8fbe\u5f0f\u7684\u7ed3\u679c\u6216\u4ece\u53d8\u91cf\u4e2d\u83b7\u53d6\u7684\u503c\u3002\u6211\u4eec\u53ef\u4ee5\u5728\u4ee5\u4e0b\u4ee3\u7801\u4e2d\u770b\u5230\u76f8\u540c\u7684\u63d2\u56fe:\n <\/div>\n
\n
function TaggedLiteral(str, val1, val2) { \n    console.log<\/span>(str); \n    console.log<\/span>(val1+\"    \"<\/span>+val2); \n} \n  \nlet text = 'Hello World'<\/span>; \nTaggedLiteral`Colors ${text} ${10+30}`;<\/pre>\n

<\/code>\n <\/div>\n

\n \u8f93\u51fa\n <\/div>\n
\n
[ 'Colors '<\/span>, ' '<\/span>, ''<\/span> ]\nHello World    40<\/pre>\n

<\/code>\n <\/div>\n

\u539f\u59cb\u5b57\u7b26\u4e32<\/h2>\n
\n \u6a21\u677f\u6587\u5b57\u539f\u59cb\u65b9\u6cd5\u5141\u8bb8\u5728\u8f93\u5165\u539f\u59cb\u5b57\u7b26\u4e32\u65f6\u8bbf\u95ee\u5b83\u4eec\u3002\u9664\u6b64\u4e4b\u5916\uff0c\u5b58\u5728 string.raw() \u65b9\u6cd5\u6765\u521b\u5efa\u539f\u59cb\u5b57\u7b26\u4e32\uff0c\u7c7b\u4f3c\u4e8e\u9ed8\u8ba4\u6a21\u677f\u51fd\u6570\u3002\u5b83\u5141\u8bb8\u6211\u4eec\u50cf\u5728\u6b63\u5219\u8868\u8fbe\u5f0f\u6587\u5b57\u4e2d\u90a3\u6837\u7f16\u5199\u53cd\u659c\u6760\u3002\n <\/div>\n
\n string.raw() \u65b9\u6cd5\u7528\u4e8e\u663e\u793a\u5b57\u7b26\u4e32\uff0c\u800c\u65e0\u9700\u5bf9\u53cd\u659c\u6760\u5b57\u7b26\u8fdb\u884c\u4efb\u4f55\u89e3\u91ca\u3002\u65e0\u9700\u4f7f\u7528\u5927\u91cf\u53cd\u659c\u6760\u5373\u53ef\u6253\u5370 windows \u5b50\u76ee\u5f55\u4f4d\u7f6e\u4e5f\u5f88\u6709\u7528\u3002\n <\/div>\n
\n \u793a\u4f8b\n <\/div>\n
\n
var <\/span>rawText = String.raw`Hello \\n World \\n Welcome back! ` \nconsole.log<\/span>(rawText)<\/pre>\n

<\/code>\n <\/div>\n

\n \u8f93\u51fa\n <\/div>\n
\n
Hello \\n World \\n Welcome back!<\/pre>\n

<\/code>\n <\/div>\n

String.fromCodePoint()<\/h2>\n
\n \u6b64\u65b9\u6cd5\u8fd4\u56de\u4e00\u4e2a\u5b57\u7b26\u4e32\uff0c\u8be5\u5b57\u7b26\u4e32\u662f\u4f7f\u7528\u6307\u5b9a\u7684 Unicode \u4ee3\u7801\u70b9\u5e8f\u5217\u521b\u5efa\u7684\u3002\u5982\u679c\u4f20\u9012\u4e86\u65e0\u6548\u7684\u4ee3\u7801\u70b9\uff0c\u5b83\u4f1a\u629b\u51fa\u4e00\u4e2aRangeError\u3002\n <\/div>\n
\n \u793a\u4f8b\n <\/div>\n
\n
console.log<\/span>(String.fromCodePoint<\/span>(49))        \nconsole.log<\/span>(String.fromCodePoint<\/span>(80, 76))<\/pre>\n

<\/code>\n <\/div>\n

\n \u8f93\u51fa\n <\/div>\n
\n
1\nPL<\/pre>\n

<\/code>\n <\/div>\n

<\/body>
\n<\/html><\/p>\n","protected":false},"excerpt":{"rendered":"ES6 \u6a21\u677f\u6587\u5b57zh-cn","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[161],"tags":[],"class_list":["post-1925","post","type-post","status-publish","format-standard","hentry","category-es6jc"],"_links":{"self":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts\/1925"}],"collection":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/comments?post=1925"}],"version-history":[{"count":0,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts\/1925\/revisions"}],"wp:attachment":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/media?parent=1925"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/categories?post=1925"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/tags?post=1925"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}