{"id":1913,"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 \u7bad\u5934\u51fd\u6570","status":"publish","type":"post","link":"https:\/\/bianchenghao6.com\/1913.html","title":{"rendered":"ES6 \u7bad\u5934\u51fd\u6570"},"content":{"rendered":"
\n
const functionName = (arg1, arg2, ?..) => {\n \/\/body of the function<\/span>\n}<\/pre>\n<\/code>\n <\/div>\n
\n \u7bad\u5934\u51fd\u6570\u6216 Lambda \u51fd\u6570\u7531\u4e09\u90e8\u5206\u7ec4\u6210:\n <\/div>\n \u53c2\u6570: \u4efb\u4f55\u51fd\u6570\u90fd\u53ef\u4ee5\u9009\u62e9\u6709\u53c2\u6570\u3002<\/span>
\n \u7c97\u7bad\u5934\u8868\u793a\u6cd5\/lambda \u8868\u793a\u6cd5: \u8fd9\u662f\u7bad\u5934(=>) \u7684\u8868\u793a\u6cd5\u3002<\/span>
\n \u8bed\u53e5: \u8868\u793a\u51fd\u6570\u7684\u6307\u4ee4\u96c6\u3002<\/span> <\/p>\n\n \u8ba9\u6211\u4eec\u8bd5\u7740\u7528\u4e00\u4e2a\u4f8b\u5b50\u6765\u7406\u89e3\u5b83\u3002\n <\/div>\n\n \u5728\u4e0b\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e09\u4e2a\u51fd\u6570\uff0c\u5206\u522b\u663e\u793a\u51fd\u6570\u8868\u8fbe\u5f0f\u3001\u533f\u540d\u51fd\u6570\u548c\u7bad\u5934\u51fd\u6570\u3002\n <\/div>\n\n\/\/ function expression<\/span>\nvar <\/span>myfun1 = function show() {\n console.log<\/span>(\"It is a Function Expression\"<\/span>); \n}\n\/\/ Anonymous function<\/span>\nvar <\/span>myfun2 = function () {\n console.log<\/span>(\"It is an Anonymous Function\"<\/span>); \n }\n \n\/\/Arrow function<\/span>\nvar <\/span>myfun3 = () => {\n console.log<\/span>(\"It is an Arrow Function\"<\/span>); \n };\nmyfun1();\nmyfun2();\nmyfun3();<\/pre>\n<\/code>\n <\/div>\n
\n \u8f93\u51fa\n <\/div>\n\nIt is a Function Expression\nIt is an Anonymous Function\nIt is an Arrow Function<\/pre>\n<\/code>\n <\/div>\n
Syntactic Variations<\/h3>\n\n \u7bad\u5934\u51fd\u6570\u6709\u4e00\u4e9b\u8bed\u6cd5\u53d8\u5316\uff0c\u5982\u4e0b\u6240\u793a:\n <\/div>\n \u5355\u4e2a\u53c2\u6570\u7684\u53ef\u9009\u62ec\u53f7<\/span> <\/p>\n\nvar <\/span>num = x => {\n console.log<\/span>(x);\n}\nnum(140);<\/pre>\n<\/code>\n <\/div>\n
\n \u8f93\u51fa\n <\/div>\n\n140<\/pre>\n<\/code>\n <\/div>\n
\u5355\u6761\u8bed\u53e5\u7684\u53ef\u9009\u5927\u62ec\u53f7\uff0c\u5982\u679c\u4e0d\u9700\u8981\u4efb\u4f55\u53c2\u6570\uff0c\u5219\u4e3a\u7a7a\u5927\u62ec\u53f7\u3002<\/span> <\/p>\n\nvar <\/span>show = () => console.log<\/span>(\"Hello World\"<\/span>);\nshow();<\/pre>\n<\/code>\n <\/div>\n
\n \u8f93\u51fa\n <\/div>\n\nHello World<\/pre>\n<\/code>\n <\/div>\n
\u5e26\u53c2\u6570\u7684\u7bad\u5934\u51fd\u6570<\/h2>\n\n \u5982\u679c\u9700\u8981\u4f7f\u7528\u7bad\u5934\u51fd\u6570\u4f20\u9012\u591a\u4e2a\u53c2\u6570\uff0c\u5219\u5fc5\u987b\u5728\u62ec\u53f7\u5185\u4f20\u9012\u5b83\u4eec\u3002\n <\/div>\n\n \u4f8b\u5982\n <\/div>\n\nvar <\/span>show = (a,b,c) => {\n console.log<\/span>(a + \" \"<\/span><\/span> + b + \" \" + c );\n}\nshow(100,200,300);<\/pre>\n<\/code>\n <\/div>\n
\n \u8f93\u51fa\n <\/div>\n\n100 200 300<\/pre>\n<\/code>\n <\/div>\n
\u5e26\u9ed8\u8ba4\u53c2\u6570\u7684\u7bad\u5934\u51fd\u6570<\/h2>\n\n \u5728 ES6 \u4e2d\uff0c\u8be5\u51fd\u6570\u5141\u8bb8\u4f7f\u7528\u9ed8\u8ba4\u503c\u521d\u59cb\u5316\u53c2\u6570\uff0c\u5982\u679c\u6ca1\u6709\u4f20\u9012\u7ed9\u5b83\u7684\u503c\uff0c\u6216\u8005\u672a\u5b9a\u4e49\u3002\u60a8\u53ef\u4ee5\u5728\u4ee5\u4e0b\u4ee3\u7801\u4e2d\u770b\u5230\u76f8\u540c\u7684\u63d2\u56fe:\n <\/div>\n\n \u4f8b\u5982\n <\/div>\n\nvar <\/span>show = (a, b=200) => {\n console.log<\/span>(a + \" \"<\/span> + b);\n}\nshow(100);<\/pre>\n<\/code>\n <\/div>\n
\n \u5728\u4e0a\u8ff0\u51fd\u6570\u4e2d\uff0cb\u7684\u503c\u9ed8\u8ba4\u8bbe\u7f6e\u4e3a200\u3002\u5982\u679c\u6ca1\u6709\u660e\u786e\u4f20\u9012 b \u7684\u503c\uff0c\u8be5\u51fd\u6570\u5c06\u59cb\u7ec8\u5c06 200 \u89c6\u4e3a b \u7684\u503c\u3002\n <\/div>\n\n >\u8f93\u51fa\n <\/div>\n\n100 200<\/pre>\n<\/code>\n <\/div>\n
\n \u5982\u679c\u51fd\u6570\u663e\u5f0f\u4f20\u9012\u5176\u503c\uff0c\u53c2\u6570'b'\u7684\u9ed8\u8ba4\u503c\u5c06\u88ab\u8986\u76d6\u3002\u60a8\u53ef\u4ee5\u5728\u4ee5\u4e0b\u793a\u4f8b\u4e2d\u770b\u5230\u5b83:\n <\/div>\n\n \u4f8b\u5982\n <\/div>\n\nvar <\/span>show = (a, b=200) => {\n console.log<\/span>(a + \" \"<\/span> + b);\n}\nshow(100,500);<\/pre>\n<\/code>\n <\/div>\n
\n \u8f93\u51fa\n <\/div>\n\n100 500<\/pre>\n<\/code>\n <\/div>\n
\u5e26Rest \u53c2\u6570\u7684\u7bad\u5934\u51fd\u6570<\/h2>\n\n Rest \u53c2\u6570\u4e0d\u9650\u5236\u60a8\u5728\u51fd\u6570\u4e2d\u4f20\u9012\u503c\u7684\u6570\u91cf\uff0c\u4f46\u6240\u6709\u4f20\u9012\u7684\u503c\u5fc5\u987b\u662f\u540c\u4e00\u7c7b\u578b\u3002\u6211\u4eec\u4e5f\u53ef\u4ee5\u8bf4 rest \u53c2\u6570\u5145\u5f53\u76f8\u540c\u7c7b\u578b\u7684\u591a\u4e2a\u53c2\u6570\u7684\u5360\u4f4d\u7b26\u3002\n <\/div>\n\n \u4e3a\u4e86\u58f0\u660e rest \u53c2\u6570\uff0c\u53c2\u6570\u540d\u79f0\u5e94\u8be5\u4ee5\u5177\u6709\u4e09\u4e2a\u53e5\u70b9(\u4e0d\u8d85\u8fc7\u4e09\u4e2a\u6216\u4e0d\u5c11\u4e8e\u4e09\u4e2a)\u3002\n <\/div>\n\n \u60a8\u53ef\u4ee5\u5728\u4ee5\u4e0b\u793a\u4f8b\u4e2d\u770b\u5230\u76f8\u540c\u7684\u63d2\u56fe:\n <\/div>\n\nvar <\/span>show = (a, ...args) => {\n console.log<\/span>(a + \" \"<\/span> + args);\n}\nshow(100,200,300,400,500,600,700,800);<\/pre>\n<\/code>\n <\/div>\n
\n \u8f93\u51fa\n <\/div>\n\n100 200,300,400,500,600,700,800<\/pre>\n<\/code>\n <\/div>\n
\u4e0d\u5e26\u62ec\u53f7\u7684\u7bad\u5934\u51fd\u6570<\/h2>\n\n \u5982\u679c\u8981\u4f20\u9012\u5355\u4e2a\u53c2\u6570\uff0c\u5219\u62ec\u53f7\u662f\u53ef\u9009\u7684\u3002\n <\/div>\n\n \u4f8b\u5982\n <\/div>\n\nvar <\/span>show = x => {\n console.log<\/span>(x);\n}\nshow(\"Hello World\"<\/span>); <\/pre>\n<\/code>\n <\/div>\n
\n \u8f93\u51fa\n <\/div>\n\nHello World<\/pre>\n<\/code>\n <\/div>\n
\u7bad\u5934\u51fd\u6570\u7684\u4f18\u70b9<\/h2>\n\n \u8ba9\u6211\u4eec\u5c1d\u8bd5\u8be6\u7ec6\u8bf4\u660e\u4e0a\u56fe\u4e2d\u53ef\u7528\u7684\u4f18\u70b9\u3002\n <\/div>\n\n 1.\u5b83\u51cf\u5c11\u4e86\u4ee3\u7801\u5927\u5c0f: \u5f53\u6211\u4eec\u4f7f\u7528\u7bad\u5934\u51fd\u6570\u7684\u8bed\u6cd5\u65f6\uff0c\u4ee3\u7801\u7684\u5927\u5c0f\u4f1a\u51cf\u5c11\u3002\u4f7f\u7528\u7bad\u5934\u51fd\u6570\uff0c\u6211\u4eec\u53ef\u4ee5\u7f16\u5199\u66f4\u5c11\u7684\u4ee3\u7801\u3002\n <\/div>\n\n 2. Return \u8bed\u53e5\u548cFunctional \u5927\u62ec\u53f7\u5bf9\u4e8e\u5355\u884c\u51fd\u6570\u662f\u53ef\u9009\u7684: \u5728ES5 \u4e2d\uff0c\u6211\u4eec\u9700\u8981\u5728\u51fd\u6570\u4e2d\u5b9a\u4e49return \u8bed\u53e5\uff0c\u4f46\u5728ES6 \u4e2d\uff0c\u6211\u4eec\u4e0d\u9700\u8981\u4e3a\u5355\u884c\u51fd\u6570\u5b9a\u4e49return \u8bed\u53e5\u3002\u51fd\u6570\u5927\u62ec\u53f7\u5bf9\u4e8e\u5355\u884c\u51fd\u6570\u4e5f\u662f\u53ef\u9009\u7684\u3002\n <\/div>\n\n \u4f8b\u5982\n <\/div>\n\n \u5728 ES5 \u4e2d\n <\/div>\n\nfunction show(value){\nreturn <\/span>value\/2;\n}<\/pre>\n<\/code>\n <\/div>\n
\n \u5728 ES6 \u4e2d\n <\/div>\n\nvar <\/span>show = value => value\/2;\nconsole.log<\/span>(show(100));<\/pre>\n<\/code>\n <\/div>\n
\n \u6ce8\u610f: \u5982\u679c\u4f60\u4e0d\u662f\u5728\u5355\u6761\u8bed\u53e5\u4e0a\u4f7f\u7528\u5927\u62ec\u53f7\uff0c\u90a3\u4e48\u4f60\u4e0d\u9700\u8981\u4f7f\u7528return\uff0c\u4f46\u662f\u5982\u679c\u4f60\u751a\u81f3\u5728\u5355\u6761\u8bed\u53e5\u4e0a\u4f7f\u7528\u5927\u62ec\u53f7\uff0c\u90a3\u4e48\u4f60\u5fc5\u987b\u4f7f\u7528 return \u5173\u952e\u5b57\u3002\n <\/div>\n\n \u4f60\u53ef\u4ee5\u5728\u4e0b\u9762\u7684\u4f8b\u5b50\u4e2d\u7406\u89e3:\n <\/div>\n\n \u6ca1\u6709 Return Keyword\n <\/div>\n\nvar <\/span>show = value => {\n value\/2;\n}\nconsole.log<\/span>(show(50));<\/pre>\n<\/code>\n <\/div>\n
\n \u8f93\u51fa\n <\/div>\n\nundefined<\/pre>\n<\/code>\n <\/div>\n
\n \u5e26\u8fd4\u56de\u5173\u952e\u5b57\n <\/div>\n\nvar <\/span>show = value => {\n return <\/span>value\/2;\n}\nconsole.log<\/span>(show(50));<\/pre>\n<\/code>\n <\/div>\n
\n \u8f93\u51fa\n <\/div>\n\n25<\/pre>\n<\/code>\n <\/div>\n
\n 3.\u8bcd\u6cd5\u7ed1\u5b9a\u4e0a\u4e0b\u6587: \u7bad\u5934\u51fd\u6570\u8bcd\u6cd5\u6216\u9759\u6001\u7ed1\u5b9a\u4e0a\u4e0b\u6587\u3002\u4e0e\u5e38\u89c4\u51fd\u6570\u76f8\u6bd4\uff0c\u7bad\u5934\u51fd\u6570\u4e2d this \u7684\u5904\u7406\u662f\u4e0d\u540c\u7684\u3002\u5728\u7bad\u5934\u51fd\u6570\u4e2d\uff0c\u6ca1\u6709\u4efb\u4f55this\u7ed1\u5b9a\u3002\u5728\u5e38\u89c4\u51fd\u6570\u4e2d\uff0cthis\u5173\u952e\u5b57\u7528\u4e8e\u8868\u793a\u8c03\u7528\u8be5\u51fd\u6570\u7684\u5bf9\u8c61\uff0c\u53ef\u4ee5\u662f\u4e00\u4e2a\u7a97\u53e3\u3001\u6309\u94ae\u3001\u6587\u6863\u6216\u4efb\u4f55\u4e1c\u897f\u3002\n <\/div>\n\n \u4f46\u662f\u5bf9\u4e8e\u7bad\u5934\u51fd\u6570\uff0c\u8fd9\u4e2a\u5173\u952e\u5b57\u603b\u662f\u4ee3\u8868\u5b9a\u4e49\u7bad\u5934\u51fd\u6570\u7684\u5bf9\u8c61\u3002\n <\/div>\n\n \u8ba9\u6211\u4eec\u8bd5\u7740\u7528\u4e0b\u9762\u7684\u4e00\u6bb5\u4ee3\u7801\u6765\u7406\u89e3\u5b83:\n <\/div>\n\n \u4f8b\u5982\n <\/div>\n\n \u8003\u8651\u4e00\u4e2a\u7c7b\u6709\u4e00\u4e2a\u6570\u5b57\u6570\u7ec4\uff0c\u5982\u679c\u6570\u5b57\u5c0f\u4e8e30\uff0c\u6211\u4eec\u5c06\u628a\u5b83\u4eec\u63a8\u5165\u5b50\u961f\u5217\u3002\n <\/div>\n\n \u5728ES5\u4e2d\uff0c\u53ef\u4ee5\u8fd9\u6837\u505a\n <\/div>\n\nthis<\/span>.num.forEach<\/span>(function(num) { \n if <\/span>(num < 30) \n this<\/span>.child.push<\/span>(num); \n}.bind<\/span>(this<\/span>));<\/pre>\n<\/code>\n <\/div>\n
\n \u5728ES6\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u7bad\u5934\u51fd\u6570\u6765\u5b9e\u73b0\n <\/div>\n\nthis<\/span>.num.forEach<\/span>((num) => { \n if <\/span>(num < 30) \n this<\/span>.child.push<\/span>(num); \n});<\/pre>\n<\/code>\n <\/div>\n
\n \u6240\u4ee5\uff0c\u901a\u8fc7\u4f7f\u7528\u7bad\u5934\u51fd\u6570\uff0c\u6211\u4eec\u4e0d\u9700\u8981\u9690\u5f0f\u7ed1\u5b9a\u5b83\uff0c\u56e0\u4e3a\u5b83\u662f\u7531\u7bad\u5934\u51fd\u6570\u81ea\u52a8\u6267\u884c\u7684\u3002\n <\/div>\n\n \u6b63\u5982\u6211\u4eec\u6240\u89c1\uff0c\u7bad\u5934\u51fd\u6570\u4f7f\u5f97\u51fd\u6570\u7684\u7f16\u5199\u66f4\u5c11\u590d\u6742\uff0c\u540c\u65f6\u4e5f\u51cf\u5c11\u4e86\u884c\u6570\u3002\n <\/div>\n <\/body>
\n<\/html><\/p>\n","protected":false},"excerpt":{"rendered":"ES6 \u7bad\u5934\u51fd\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-1913","post","type-post","status-publish","format-standard","hentry","category-es6jc"],"_links":{"self":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts\/1913"}],"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=1913"}],"version-history":[{"count":0,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts\/1913\/revisions"}],"wp:attachment":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/media?parent=1913"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/categories?post=1913"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/tags?post=1913"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}