{"id":1907,"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 Rest\u53c2\u6570","status":"publish","type":"post","link":"https:\/\/bianchenghao6.com\/1907.html","title":{"rendered":"ES6 Rest\u53c2\u6570"},"content":{"rendered":"


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

\n

ES6 Rest\u53c2\u6570<\/h1>\n<\/p><\/div>\n

ES6 Rest\u53c2\u6570<\/h2>\n
\n \u5728 ECMAScript 2015 \u6216 ES6 \u4e2d\u5f15\u5165\u4e86 rest \u53c2\u6570\uff0c\u8fd9\u63d0\u9ad8\u4e86\u5904\u7406\u53c2\u6570\u7684\u80fd\u529b\u3002 rest \u53c2\u6570\u5141\u8bb8\u6211\u4eec\u5c06\u65e0\u9650\u6570\u91cf\u7684\u53c2\u6570\u8868\u793a\u4e3a\u4e00\u4e2a\u6570\u7ec4\u3002\u901a\u8fc7\u4f7f\u7528 rest \u53c2\u6570\uff0c\u53ef\u4ee5\u4f7f\u7528\u4efb\u610f\u6570\u91cf\u7684\u53c2\u6570\u8c03\u7528\u51fd\u6570\u3002\n <\/div>\n
\n \u5728 ES6 \u4e4b\u524d\uff0c\u4f7f\u7528\u51fd\u6570\u7684 arguments \u5bf9\u8c61\u3002 arguments \u5bf9\u8c61\u4e0d\u662f Array \u7c7b\u578b\u7684\u5b9e\u4f8b\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u4e0d\u80fd\u76f4\u63a5\u4f7f\u7528 filter() \u65b9\u6cd5\u3002\n <\/div>\n
\n rest \u53c2\u6570\u4ee5\u4e09\u4e2a\u70b9(...) \u4e3a\u524d\u7f00\u3002\u867d\u7136 rest \u53c2\u6570\u7684\u8bed\u6cd5\u7c7b\u4f3c\u4e8e\u6269\u5c55\u8fd0\u7b97\u7b26\uff0c\u4f46\u5b83\u4e0e\u6269\u5c55\u8fd0\u7b97\u7b26\u5b8c\u5168\u76f8\u53cd\u3002 rest \u53c2\u6570\u5fc5\u987b\u662f\u6700\u540e\u4e00\u4e2a\u53c2\u6570\uff0c\u56e0\u4e3a\u5b83\u7528\u4e8e\u5c06\u6240\u6709\u5269\u4f59\u5143\u7d20\u6536\u96c6\u5230\u4e00\u4e2a\u6570\u7ec4\u4e2d\u3002\n <\/div>\n

\u8bed\u6cd5<\/h3>\n
\n
function fun(a, b, ...theArgs) {\n  \/\/ statements<\/span>\n}<\/pre>\n

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

\u793a\u4f8b<\/h3>\n
\n
function show(...args) {\n  let sum = 0;\n  for <\/span>(let i of args) {\n      sum += i;\n  }\n  console.log<\/span>(\"Sum = \"<\/span>+sum);\n}\nshow(10, 20, 30);<\/pre>\n

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

\n \u8f93\u51fa\n <\/div>\n
\n
Sum = 60<\/pre>\n

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

\n \u6211\u4eec\u5728\u51fd\u6570\u4e2d\u4f20\u9012\u7684\u6240\u6709\u53c2\u6570\u90fd\u5c06\u6620\u5c04\u5230\u53c2\u6570\u5217\u8868\u3002\u5982\u4e0a\u6240\u8ff0\uff0crest \u53c2\u6570(...) \u5e94\u59cb\u7ec8\u4f4d\u4e8e\u53c2\u6570\u5217\u8868\u7684\u6700\u540e\u3002\u5982\u679c\u6211\u4eec\u628a\u5b83\u653e\u5728\u5176\u4ed6\u4efb\u4f55\u5730\u65b9\uff0c\u5c31\u4f1a\u5bfc\u81f4\u9519\u8bef\u3002\n <\/div>\n
\n \u5177\u6709\u4ee5\u4e0b\u8bed\u6cd5\u7684rest\u53c2\u6570\u5c06\u5bfc\u81f4\u9519\u8bef\u3002\n <\/div>\n
\n
function show(a,...rest, b) {\n  \/\/ error<\/span>\n };<\/pre>\n

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

Rest Parameter \u548carguments \u5bf9\u8c61\u7684\u533a\u522b<\/h2>\n
\n \u5176\u4f59parameter \u548carguments \u5bf9\u8c61\u5f7c\u6b64\u4e0d\u540c\u3002\u8ba9\u6211\u4eec\u770b\u770brest\u53c2\u6570\u548carguments\u5bf9\u8c61\u7684\u533a\u522b:\n <\/div>\n

arguments \u5bf9\u8c61\u662f\u4e00\u4e2a\u7c7b\u6570\u7ec4(\u4f46\u4e0d\u662f\u6570\u7ec4)\uff0c\u800c\u5176\u4f59\u53c2\u6570\u662f\u6570\u7ec4\u5b9e\u4f8b\u3002\u53c2\u6570\u5bf9\u8c61\u4e0d\u5305\u62ecsort\u3001map\u3001forEach\u3001\u6216pop\u7b49\u65b9\u6cd5\uff0c\u4f46\u8fd9\u4e9b\u65b9\u6cd5\u53ef\u4ee5\u76f4\u63a5\u5728rest\u53c2\u6570\u4e2d\u4f7f\u7528\u3002<\/span> <\/p>\n

\u4f11\u606f\u53c2\u6570\u548c\u89e3\u6784<\/h2>\n
\n \u89e3\u6784\u610f\u5473\u7740\u5c06\u590d\u6742\u7684\u7ed3\u6784\u5206\u89e3\u6210\u66f4\u7b80\u5355\u7684\u90e8\u5206\u3002\u6211\u4eec\u53ef\u4ee5\u5b9a\u4e49\u4e00\u4e2a\u6570\u7ec4\u4f5c\u4e3a rest \u53c2\u6570\u3002\u4f20\u5165\u7684\u53c2\u6570\u5c06\u5206\u89e3\u4e3a\u6570\u7ec4\u3002 Rest\u53c2\u6570\u4ec5\u652f\u6301\u6570\u7ec4\u89e3\u6784\u3002\n <\/div>\n
\n \u901a\u8fc7\u4f7f\u7528rest\u53c2\u6570\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u6570\u7ec4\u7684\u6240\u6709\u5269\u4f59\u5143\u7d20\u653e\u5165\u4e00\u4e2a\u65b0\u6570\u7ec4\u4e2d\u3002\n <\/div>\n
\n \u8ba9\u6211\u4eec\u770b\u4e00\u4e0b\u76f8\u540c\u7684\u8bf4\u660e\u3002\n <\/div>\n

\u793a\u4f8b<\/h3>\n
\n
var <\/span>colors = [\"Violet\"<\/span>, \"Indigo\"<\/span>, \"Blue\"<\/span>, \"Green\"<\/span>, \"Yellow\"<\/span>, \"Orange\"<\/span>, \"Red\"<\/span>];  \n  \n\/\/ destructuring assignment  <\/span>\nvar <\/span>[a,b,...args] = colors;  \nconsole.log<\/span>(a);   \nconsole.log<\/span>(b);   \nconsole.log<\/span>(args);<\/pre>\n

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

\n \u8f93\u51fa\n <\/div>\n
\n
Violet\nIndigo\n[ 'Blue'<\/span>, 'Green'<\/span>, 'Yellow'<\/span>, 'Orange'<\/span>, 'Red'<\/span> ]<\/pre>\n

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

\u52a8\u6001\u51fd\u6570\u4e2d\u7684\u4f11\u606f\u53c2\u6570<\/h2>\n
\n JavaScript \u5141\u8bb8\u6211\u4eec\u4f7f\u7528 function \u6784\u9020\u51fd\u6570\u521b\u5efa\u52a8\u6001\u51fd\u6570\u3002\u6211\u4eec\u53ef\u4ee5\u5728\u52a8\u6001\u51fd\u6570\u4e2d\u4f7f\u7528 rest \u53c2\u6570\u3002\n <\/div>\n

\u793a\u4f8b<\/h3>\n
\n
let num = new <\/span>Function('...args'<\/span>,'return <\/span>args'<\/span>);\nconsole.log<\/span>(num(10, 20, 30));<\/pre>\n

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

\n \u8f93\u51fa\n <\/div>\n
\n
[ 10, 20, 30 ]<\/pre>\n

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

<\/body>
\n<\/html><\/p>\n","protected":false},"excerpt":{"rendered":"ES6 Rest\u53c2\u6570zh-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-1907","post","type-post","status-publish","format-standard","hentry","category-es6jc"],"_links":{"self":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts\/1907"}],"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=1907"}],"version-history":[{"count":0,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts\/1907\/revisions"}],"wp:attachment":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/media?parent=1907"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/categories?post=1907"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/tags?post=1907"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}