{"id":1302,"date":"2022-06-20T16:51:58","date_gmt":"2022-06-20T08:51:58","guid":{"rendered":"https:\/\/zhuxinyong.com\/?p=1302"},"modified":"2022-06-22T15:02:29","modified_gmt":"2022-06-22T07:02:29","slug":"shu-zu-jie-gou","status":"publish","type":"post","link":"https:\/\/zhuxinyong.com\/?p=1302","title":{"rendered":"41 &#8211; \u6570\u7ec4\u89e3\u6784"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/zhuxinyong.com\/wp-content\/uploads\/2022\/06\/16557151238736.jpg\" alt=\"\" \/><\/p>\n<p>\u539f\u6587\u5730\u5740\uff1a<a href=\"https:\/\/dev.to\/bhagatparwinder\/destructuring-arrays-1dkf\" target=\"_blank\" rel=\"noopener\">https:\/\/dev.to\/bhagatparwinder\/destructuring-arrays-1dkf<\/a><\/p>\n<p>\u89e3\u6784\u6216\u8005\u89e3\u6784\u8d4b\u503c\u662f\u4e00\u4e2a\u8ba9\u6211\u4eec\u53ef\u4ee5\u5bf9\u6570\u7ec4\u6216\u5bf9\u8c61\u8fdb\u884c\u62c6\u5305\uff0c\u7136\u540e\u628a\u5b83\u4eec\u8d4b\u503c\u7ed9\u53d8\u91cf\u7684\u8bed\u6cd5\u3002\u8fd9\u7bc7\u6587\u7ae0\u5c06\u91cd\u6e29\u4e00\u4e0b\u6570\u7ec4\u89e3\u6784\u3002<\/p>\n<p>\u4e3a\u4e86\u6f14\u793a\uff0c\u6211\u4eec\u4e00\u8d77\u6765\u770b\u4e00\u4e2a\u4f8b\u5b50\u3002\u6211\u4eec\u5c06\u521b\u5efa\u4e00\u4e2a\u51fd\u6570\u7136\u540e\u63a5\u53d7\u6570\u5b57\u6570\u7ec4\u5e76\u6253\u5370\u90a3\u4e9b\u6570\u5b57\u3002<\/p>\n<pre class=\"line-numbers\"><code class=\"language-plain_text\">const myNumbers = (arrOfNumbers) =&gt; {\n    const a = arrOfNumbers[0];\n    const b = arrOfNumbers[1];\n    const c = arrOfNumbers[2];\n    const d = arrOfNumbers[3];\n    const e = arrOfNumbers[4];\n    const f = arrOfNumbers[5];\n    const g = arrOfNumbers[6];\n\n    console.log(a, b, c, d, e, f, g)\n}\n\nmyNumbers([7, 2, 19, 4000, 12, 45, -17]); \/\/ 7 2 19 4000 12 45 -17\n<\/code><\/pre>\n<p>\u4e0a\u9762\u7684\u4ee3\u7801\u6ca1\u6709\u95ee\u9898\uff0c\u4f46\u662f\u6211\u4eec\u5fc5\u987b\u4e3a\u6570\u7ec4\u7684\u6bcf\u4e2a\u5143\u7d20\u8d4b\u503c\u4e00\u4e2a\u53d8\u91cf\uff0c\u6709\u5f88\u591a\u91cd\u590d\u4ee3\u7801\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528\u5faa\u73af\u6765\u51cf\u5c11\u4e00\u4e9b\u4ee3\u7801\uff1a<\/p>\n<pre class=\"line-numbers\"><code class=\"language-plain_text\">const myNumbers = (arrOfNumbers) =&gt; {\n    arrOfNumbers.forEach((value) =&gt; {\n        console.log(value);\n    })\n}\n\nmyNumbers([7, 2, 19, 4000, 12, 45, -17]); \/\/ 7, 2, 19, 4000, 12, 45, -17\n<\/code><\/pre>\n<p>\u5faa\u73af\u6ca1\u6709\u95ee\u9898\u4f46\u662f\u6211\u4eec\u5374\u5411\u4ee3\u7801\u4e2d\u6dfb\u52a0\u4e86\u903b\u8f91\u3002<\/p>\n<p>\u89e3\u6784\u4f7f\u4ee3\u7801\u7b80\u6d01\uff1a<\/p>\n<pre class=\"line-numbers\"><code class=\"language-plain_text\">const myNumbers = (arrOfNumbers) =&gt; {\n    const [a, b, c, d, e, f, g] = arrOfNumbers;\n    console.log(a, b, c, d, e, f, g); \/\/ 7 2 19 4000 12 45 -17\n}\n\nmyNumbers([7, 2, 19, 4000, 12, 45, -17]);\n<\/code><\/pre>\n<p>\u5c31\u662f\u8fd9\u4e48\u7b80\u5355\uff0c\u89e3\u6784\u4f7f\u5de6\u4fa7\u7684\u8868\u8fbe\u5f0f\u4e0e\u53f3\u4fa7\u5bf9\u5e94\u5e76\u6309\u987a\u5e8f\u8d4b\u503c\u3002<\/p>\n<h2><a id=\"%E4%BD%BF%E7%94%A8%E9%BB%98%E8%AE%A4%E5%80%BC\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>\u4f7f\u7528\u9ed8\u8ba4\u503c<\/h2>\n<p>\u5de6\u53f3\u4e24\u4fa7\u5e76\u4e0d\u603b\u662f\u957f\u5ea6\u5bf9\u7b49\uff0c\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\u6211\u4eec\u53ef\u4ee5\u8d4b\u4e88\u9ed8\u8ba4\u503c\uff1a<\/p>\n<pre class=\"line-numbers\"><code class=\"language-plain_text\">let a, b;\n\n[a=19, b=-17] = [1];\nconsole.log(a); \/\/ 1\nconsole.log(b); \/\/ -17\n<\/code><\/pre>\n<p>\u4e3a a \u8d4b\u503c 1\uff0c\u7531\u4e8e\u6570\u7ec4\u53ea\u6709\u4e00\u4e2a\u503c\uff0c\u6240\u4ee5 b \u53ea\u80fd\u53d6\u9ed8\u8ba4\u503c -17\u3002\u5f53\u53f3\u4fa7\u6709\u989d\u5916\u7684\u503c\u65f6\uff0c\u5c06\u4f1a\u88ab\u5ffd\u7565\u3002<\/p>\n<pre class=\"line-numbers\"><code class=\"language-plain_text\">let a, b;\n\n[a = 19, b = -17] = [1, 2, 3,];\nconsole.log(a); \/\/ 1\nconsole.log(b); \/\/ 2\n<\/code><\/pre>\n<h2><a id=\"%E4%B8%8D%E4%BD%BF%E7%94%A8%E4%B8%B4%E6%97%B6%E5%8F%98%E9%87%8F%E6%9D%A5%E4%BA%A4%E6%8D%A2%E5%8F%98%E9%87%8F\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>\u4e0d\u4f7f\u7528\u4e34\u65f6\u53d8\u91cf\u6765\u4ea4\u6362\u53d8\u91cf<\/h2>\n<pre class=\"line-numbers\"><code class=\"language-plain_text\">let a = 5;\nlet b = 15;\n\n[a, b] = [b, a];\nconsole.log(a); \/\/ 15\nconsole.log(b); \/\/ 5\n<\/code><\/pre>\n<h2><a id=\"%E5%87%BD%E6%95%B0%E8%BF%94%E5%9B%9E%E5%80%BC%E4%BD%BF%E7%94%A8%E8%A7%A3%E6%9E%84\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>\u51fd\u6570\u8fd4\u56de\u503c\u4f7f\u7528\u89e3\u6784<\/h2>\n<pre class=\"line-numbers\"><code class=\"language-plain_text\">function foo() {\n  return [1, 2];\n}\n\nlet a, b;\n[a, b] = foo();\nconsole.log(a); \/\/ 1\nconsole.log(b); \/\/ 2\n<\/code><\/pre>\n<h2><a id=\"%E5%BF%BD%E7%95%A5%E7%89%B9%E5%AE%9A%E7%9A%84%E5%80%BC\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>\u5ffd\u7565\u7279\u5b9a\u7684\u503c<\/h2>\n<p>\u6709\u65f6\u5019\u4f60\u60f3\u83b7\u53d6\u7684\u503c\u5e76\u4e0d\u662f\u8fde\u7eed\u7684\uff0c\u6211\u4eec\u53ef\u4ee5\u8df3\u8fc7\u4e2d\u95f4\u7684\u503c\uff1a<\/p>\n<pre class=\"line-numbers\"><code class=\"language-plain_text\">function foo() {\n    return [1, 2, 3, 4];\n}\n\nlet a, b;\n[a, , , b] = foo();\nconsole.log(a); \/\/ 1\nconsole.log(b); \/\/ 4\n<\/code><\/pre>\n<h2><a id=\"%E5%AD%97%E7%AC%A6%E4%B8%B2%E4%BD%BF%E7%94%A8%E8%A7%A3%E6%9E%84\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>\u5b57\u7b26\u4e32\u4f7f\u7528\u89e3\u6784<\/h2>\n<p>\u5f53\u6211\u4eec\u628a\u89e3\u6784\u548c\u5b57\u7b26\u4e32\u4e00\u8d77\u4f7f\u7528\u7684\u65f6\u5019\uff0c<code>split<\/code> \u65b9\u6cd5\u5f88\u65b9\u4fbf\u3002<\/p>\n<pre class=\"line-numbers\"><code class=\"language-plain_text\">const [firstName, lastName] = &quot;Parwinder Bhagat&quot;.split(' ');\nconsole.log(firstName); \/\/ Parwinder\nconsole.log(lastName); \/\/ Bhagat\n<\/code><\/pre>\n<h2><a id=\"%E4%B8%BA%E5%AF%B9%E8%B1%A1%E5%B1%9E%E6%80%A7%E8%B5%8B%E5%80%BC\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>\u4e3a\u5bf9\u8c61\u5c5e\u6027\u8d4b\u503c<\/h2>\n<pre class=\"line-numbers\"><code class=\"language-plain_text\">let user = {};\n[user.firstName, user.lastName] = [&quot;Parwinder&quot;, &quot;Bhagat&quot;];\n\nconsole.log(user); \/\/ { firstName: 'Parwinder', lastName: 'Bhagat' }\n<\/code><\/pre>\n<h2><a id=\"%E8%A7%A3%E6%9E%84%E4%B8%8E%E6%89%A9%E5%B1%95%E8%BF%90%E7%AE%97%E7%AC%A6\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>\u89e3\u6784\u4e0e\u6269\u5c55\u8fd0\u7b97\u7b26<\/h2>\n<p>\u5982\u679c\u4f60\u60f3\u524d\u9762\u51e0\u4e2a\u503c\u5355\u72ec\u8d4b\u503c\uff0c\u540e\u9762\u7684\u60f3\u5f52\u4e3a\u4e00\u8d77\uff0c\u53ef\u4ee5\u4f7f\u7528\u89e3\u6784\u548c\u6269\u5c55\u8fd0\u7b97\u7b26\u4e00\u8d77\u4f7f\u7528\u6765\u8fbe\u5230\u6548\u679c\uff1a<\/p>\n<pre class=\"line-numbers\"><code class=\"language-plain_text\">let [name1, name2, ...remaining] = [&quot;Parwinder&quot;, &quot;Lauren&quot;, &quot;George&quot;, &quot;Eliu&quot;, &quot;Gaurav&quot;];\n\nconsole.log(name1); \/\/ Parwinder\nconsole.log(name2); \/\/ Lauren\n\nconsole.log(remaining.length); \/\/ 3\nconsole.log(remaining[0]); \/\/ George\nconsole.log(remaining); \/\/ [ 'George', 'Eliu', 'Gaurav' ]\n<\/code><\/pre>\n<p><img decoding=\"async\" src=\"https:\/\/zhuxinyong.com\/wp-content\/uploads\/2022\/06\/\u524d\u7aef\u9ed1\u677f\u62a5-9.jpg\" alt=\"\u524d\u7aef\u9ed1\u677f\u62a5\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u539f\u6587\u5730\u5740\uff1ahttps:\/\/dev.to\/bhagatparwinder\/destructuring-&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2,20,3],"tags":[159,6],"class_list":["post-1302","post","type-post","status-publish","format-standard","hentry","category-all","category-frontend","category-tech","tag-destructuring","tag-javascript"],"_links":{"self":[{"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/posts\/1302","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1302"}],"version-history":[{"count":2,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/posts\/1302\/revisions"}],"predecessor-version":[{"id":1310,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/posts\/1302\/revisions\/1310"}],"wp:attachment":[{"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1302"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1302"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1302"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}