{"id":58373,"date":"2023-10-24T16:01:29","date_gmt":"2023-10-24T08:01:29","guid":{"rendered":"https:\/\/docs.pingcode.com\/?p=58373"},"modified":"2023-10-24T16:07:24","modified_gmt":"2023-10-24T08:07:24","slug":"es6%e6%96%b0%e7%89%b9%e6%80%a7%e6%9c%89%e5%93%aa%e4%ba%9b","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/58373.html","title":{"rendered":"ES6\u65b0\u7279\u6027\u6709\u54ea\u4e9b"},"content":{"rendered":"\n<blockquote class=\"wp-block-quote\">\n<p>ES6\u65b0\u7279\u6027\u6709\uff1a1\u3001\u5757\u7ea7\u4f5c\u7528\u57df\uff1b2\u3001\u7bad\u5934\u51fd\u6570\uff1b3\u3001\u89e3\u6784\u8d4b\u503c\uff1b4\u3001\u6a21\u677f\u5b57\u7b26\u4e32\uff1b5\u3001\u9ed8\u8ba4\u53c2\u6570\u503c\uff1b6\u3001\u7c7b\u548c\u7ee7\u627f\uff1b7\u3001\u6a21\u5757\u5316\u3002ES6\u5f15\u5165\u4e86<code>let<\/code>\u548c<code>const<\/code>\u5173\u952e\u5b57\uff0c\u5141\u8bb8\u53d8\u91cf\u5728\u5757\u7ea7\u4f5c\u7528\u57df\u5185\u58f0\u660e\uff0c\u800c\u4e0d\u662f\u53ea\u80fd\u5728\u51fd\u6570\u4f5c\u7528\u57df\u5185\u58f0\u660e\u3002\u8fd9\u63d0\u4f9b\u4e86\u66f4\u597d\u7684\u53d8\u91cf\u5c01\u88c5\u548c\u63a7\u5236\uff0c\u6709\u52a9\u4e8e\u907f\u514d\u53d8\u91cf\u6c61\u67d3\u3002<\/p>\n<\/blockquote>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e00\u3001\u5757\u7ea7\u4f5c\u7528\u57df<\/h2>\n\n\n\n<p>ES6\u5f15\u5165\u4e86<code>let<\/code>\u548c<code>const<\/code>\u5173\u952e\u5b57\uff0c\u5141\u8bb8\u53d8\u91cf\u5728\u5757\u7ea7\u4f5c\u7528\u57df\u5185\u58f0\u660e\uff0c\u800c\u4e0d\u662f\u53ea\u80fd\u5728\u51fd\u6570\u4f5c\u7528\u57df\u5185\u58f0\u660e\u3002\u8fd9\u63d0\u4f9b\u4e86\u66f4\u597d\u7684\u53d8\u91cf\u5c01\u88c5\u548c\u63a7\u5236\uff0c\u6709\u52a9\u4e8e\u907f\u514d\u53d8\u91cf\u6c61\u67d3\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>{\n  let x = 10;\n  const y = 20;\n}\n\nconsole.log(x); \/\/ \u62a5\u9519\uff0cx\u4e0d\u5728\u4f5c\u7528\u57df\u5185\nconsole.log(y); \/\/ \u62a5\u9519\uff0cy\u4e0d\u5728\u4f5c\u7528\u57df\u5185\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e8c\u3001\u7bad\u5934\u51fd\u6570<\/h2>\n\n\n\n<p>\u7bad\u5934\u51fd\u6570\u662f\u4e00\u79cd\u66f4\u7b80\u6d01\u7684\u51fd\u6570\u58f0\u660e\u65b9\u5f0f\uff0c\u5177\u6709\u9690\u5f0f\u8fd4\u56de\u548c\u7ed1\u5b9a\u5916\u90e8\u4e0a\u4e0b\u6587\u7684\u7279\u70b9\u3002\u5b83\u4eec\u901a\u5e38\u7528\u4e8e\u56de\u8c03\u51fd\u6570\u548c\u7b80\u5316\u51fd\u6570\u8868\u8fbe\u5f0f\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ \u4f20\u7edf\u51fd\u6570\nfunction add(a, b) {\n  return a + b;\n}\n\n\/\/ \u7bad\u5934\u51fd\u6570\nconst add = (a, b) =&gt; a + b;\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e09\u3001\u89e3\u6784\u8d4b\u503c<\/h2>\n\n\n\n<p>\u89e3\u6784\u8d4b\u503c\u5141\u8bb8\u60a8\u4ece\u6570\u7ec4\u6216\u5bf9\u8c61\u4e2d\u63d0\u53d6\u503c\u5e76\u5c06\u5b83\u4eec\u5206\u914d\u7ed9\u53d8\u91cf\uff0c\u4ee5\u4fbf\u66f4\u65b9\u4fbf\u5730\u8bbf\u95ee\u548c\u4f7f\u7528\u6570\u636e\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ \u6570\u7ec4\u89e3\u6784\nconst &#91;x, y] = &#91;1, 2];\n\n\/\/ \u5bf9\u8c61\u89e3\u6784\nconst { name, age } = { name: 'Alice', age: 30 };\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u56db\u3001\u6a21\u677f\u5b57\u7b26\u4e32<\/h2>\n\n\n\n<p>\u6a21\u677f\u5b57\u7b26\u4e32\u5141\u8bb8\u60a8\u5728\u5b57\u7b26\u4e32\u4e2d\u63d2\u5165\u8868\u8fbe\u5f0f\uff0c\u5e76\u66f4\u5bb9\u6613\u521b\u5efa\u591a\u884c\u5b57\u7b26\u4e32\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const name = 'Alice';\nconst message = `Hello, ${name}!\nThis is a multiline string.`;\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e94\u3001\u9ed8\u8ba4\u53c2\u6570\u503c<\/h2>\n\n\n\n<p>ES6\u5f15\u5165\u4e86\u9ed8\u8ba4\u53c2\u6570\u503c\uff0c\u5141\u8bb8\u60a8\u5728\u51fd\u6570\u58f0\u660e\u4e2d\u6307\u5b9a\u53c2\u6570\u7684\u9ed8\u8ba4\u503c\uff0c\u7b80\u5316\u4e86\u51fd\u6570\u7684\u4f7f\u7528\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>function greet(name = 'Guest') {\n  console.log(`Hello, ${name}!`);\n}\n\ngreet(); \/\/ Hello, Guest!\ngreet('Bob'); \/\/ Hello, Bob!\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u516d\u3001\u7c7b\u548c\u7ee7\u627f<\/h2>\n\n\n\n<p>ES6\u5f15\u5165\u4e86<code>class<\/code>\u5173\u952e\u5b57\uff0c\u4f7f\u5f97\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u66f4\u52a0\u6e05\u6670\u548c\u6613\u4e8e\u7406\u89e3\u3002\u5b83\u4e5f\u652f\u6301\u7c7b\u7684\u7ee7\u627f\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Animal {\n  constructor(name) {\n    this.name = name;\n  }\n\n  speak() {\n    console.log(`${this.name} makes a sound.`);\n  }\n}\n\nclass Dog extends Animal {\n  speak() {\n    console.log(`${this.name} barks.`);\n  }\n}\n\nconst dog = new Dog('Buddy');\ndog.speak(); \/\/ Buddy barks.\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e03\u3001\u6a21\u5757\u5316<\/h2>\n\n\n\n<p>ES6\u5f15\u5165\u4e86\u6a21\u5757\u5316\u7cfb\u7edf\uff0c\u5141\u8bb8\u60a8\u5c06\u4ee3\u7801\u5206\u5272\u6210\u591a\u4e2a\u6587\u4ef6\uff0c\u5e76\u901a\u8fc7<code>import<\/code>\u548c<code>export<\/code>\u8bed\u53e5\u5728\u6587\u4ef6\u4e4b\u95f4\u5171\u4eab\u529f\u80fd\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ math.js\nexport function add(a, b) {\n  return a + b;\n}\n\n\/\/ app.js\nimport { add } from '.\/math';\nconsole.log(add(2, 3)); \/\/ 5\n<\/code><\/pre>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img fetchpriority=\"high\" decoding=\"async\" width=\"484\" height=\"300\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2023\/10\/\u6a21\u677f2-484-x-300-\u50cf\u7d20.png?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"\" class=\"wp-image-58394\"\/><\/figure><\/div>\n\n\n<h2 class=\"wp-block-heading\"><strong>\u5e38\u89c1\u95ee\u7b54<\/strong><\/h2>\n\n\n\n<ol>\n<li><strong><strong>ES6\u4e2d\u7684\u5757\u7ea7\u4f5c\u7528\u57df\u662f\u4ec0\u4e48\uff1f<\/strong><\/strong>\n<ul>\n<li>ES6\u5f15\u5165\u4e86<code>let<\/code>\u548c<code>const<\/code>\u5173\u952e\u5b57\uff0c\u5141\u8bb8\u53d8\u91cf\u5728\u5757\u7ea7\u4f5c\u7528\u57df\u5185\u58f0\u660e\uff0c\u800c\u4e0d\u662f\u53ea\u80fd\u5728\u51fd\u6570\u4f5c\u7528\u57df\u5185\u58f0\u660e\u3002\u8fd9\u610f\u5473\u7740\u53d8\u91cf\u7684\u4f5c\u7528\u8303\u56f4\u53ef\u4ee5\u66f4\u7cbe\u786e\u5730\u63a7\u5236\uff0c\u5e76\u4e14\u4e0d\u4f1a\u6c61\u67d3\u5168\u5c40\u4f5c\u7528\u57df\u3002<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong><strong>\u7bad\u5934\u51fd\u6570\u6709\u4ec0\u4e48\u7279\u70b9\uff1f<\/strong><\/strong>\n<ul>\n<li>\u7bad\u5934\u51fd\u6570\u662f\u4e00\u79cd\u66f4\u7b80\u6d01\u7684\u51fd\u6570\u58f0\u660e\u65b9\u5f0f\uff0c\u5177\u6709\u9690\u5f0f\u8fd4\u56de\u548c\u7ed1\u5b9a\u5916\u90e8\u4e0a\u4e0b\u6587\u7684\u7279\u70b9\u3002\u5b83\u4eec\u901a\u5e38\u7528\u4e8e\u56de\u8c03\u51fd\u6570\u548c\u7b80\u5316\u51fd\u6570\u8868\u8fbe\u5f0f\u3002<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong><strong>ES6\u4e2d\u7684\u6a21\u5757\u5316\u662f\u4ec0\u4e48\uff1f<\/strong><\/strong>\n<ul>\n<li>ES6\u5f15\u5165\u4e86\u6a21\u5757\u5316\u7cfb\u7edf\uff0c\u5141\u8bb8\u5c06\u4ee3\u7801\u5206\u5272\u6210\u591a\u4e2a\u6587\u4ef6\uff0c\u5e76\u901a\u8fc7<code>import<\/code>\u548c<code>export<\/code>\u8bed\u53e5\u5728\u6587\u4ef6\u4e4b\u95f4\u5171\u4eab\u529f\u80fd\u3002\u8fd9\u6709\u52a9\u4e8e\u7ec4\u7ec7\u548c\u7ef4\u62a4\u5927\u578bJavaScript\u5e94\u7528\u7a0b\u5e8f\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"ES6\u65b0\u7279\u6027\u6709\uff1a1\u3001\u5757\u7ea7\u4f5c\u7528\u57df\uff1b2\u3001\u7bad\u5934\u51fd\u6570\uff1b3\u3001\u89e3\u6784\u8d4b\u503c\uff1b4\u3001\u6a21\u677f\u5b57\u7b26\u4e32\uff1b5\u3001\u9ed8\u8ba4\u53c2\u6570\u503c\uff1b6\u3001\u7c7b\u548c\u7ee7\u627f\uff1b7\u3001 [&hellip;]","protected":false},"author":6,"featured_media":58374,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/58373"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=58373"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/58373\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/58374"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=58373"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=58373"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=58373"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}