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


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

\n

ES6 \u51fd\u6570<\/h1>\n<\/p><\/div>\n

ES6 \u51fd\u6570<\/h2>\n
\n \u51fd\u6570\u662f\u4e00\u7ec4\u8f93\u5165\u8bed\u53e5\uff0c\u5b83\u6267\u884c\u7279\u5b9a\u7684\u8ba1\u7b97\u5e76\u4ea7\u751f\u8f93\u51fa\u3002\u5b83\u662f\u4e3a\u6267\u884c\u7279\u5b9a\u4efb\u52a1\u800c\u8bbe\u8ba1\u7684\u4ee3\u7801\u5757\u3002\u5b83\u5728\u88ab\u8c03\u7528(\u6216\u88ab\u8c03\u7528)\u65f6\u6267\u884c\u3002\u51fd\u6570\u5141\u8bb8\u6211\u4eec\u4ee5\u6709\u7ec4\u7ec7\u7684\u65b9\u5f0f\u91cd\u7528\u548c\u7f16\u5199\u4ee3\u7801\u3002 JavaScript \u4e2d\u7684\u51fd\u6570 \u7528\u4e8e\u6267\u884c\u64cd\u4f5c\u3002\n <\/div>\n
\n \u5728 JavaScript\uff0c\u51fd\u6570\u662f\u901a\u8fc7\u4f7f\u7528\u51fd\u6570\u5173\u952e\u5b57\u540e\u8ddf\u540d\u79f0\u548c\u62ec\u53f7()\u6765\u5b9a\u4e49\u7684\u3002\u51fd\u6570\u540d\u53ef\u4ee5\u5305\u62ec\u6570\u5b57\u3001\u5b57\u6bcd\u3001\u7f8e\u5143\u7b26\u53f7\u548c\u4e0b\u5212\u7ebf\u3002\u51fd\u6570\u540d\u4e2d\u7684\u62ec\u53f7\u53ef\u4ee5\u7531\u9017\u53f7\u5206\u9694\u7684\u53c2\u6570\u540d\u7ec4\u6210\u3002\u51fd\u6570\u4f53\u5e94\u8be5\u653e\u5728\u5927\u62ec\u53f7{}\u5185\u3002\n <\/div>\n
\n \u5b9a\u4e49\u6807\u51c6\u51fd\u6570\u7684\u8bed\u6cd5\u5982\u4e0b:\n <\/div>\n
\n
function function_name() {\n    \/\/body of the function<\/span>\n} <\/pre>\n

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

\n \u8981\u5f3a\u5236\u6267\u884c\u51fd\u6570\uff0c\u6211\u4eec\u5fc5\u987b\u8c03\u7528(\u6216\u8c03\u7528)\u51fd\u6570\u3002\u5b83\u88ab\u79f0\u4e3a\u51fd\u6570\u8c03\u7528\u3002\u8c03\u7528\u51fd\u6570\u7684\u8bed\u6cd5\u5982\u4e0b:\n <\/div>\n
\n
function_name()<\/pre>\n

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

\u793a\u4f8b<\/h3>\n
\n
function hello() \/\/defining a function<\/span>\n{\n   console.log <\/span>(\"hello function called\"<\/span>);\n   }\nhello(); \/\/calling of function<\/span><\/pre>\n

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

\n \u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u51fd\u6570hello()\u3002\u4e00\u5bf9\u62ec\u53f7{}\u5b9a\u4e49\u4e86\u51fd\u6570\u7684\u4e3b\u4f53\uff0c\u79f0\u4e3a\u4f5c\u7528\u57df\n <\/div>\n
\n \u8f93\u51fa\n <\/div>\n
\n
hello function called<\/pre>\n

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

\n \u8ba9\u6211\u4eec\u8bd5\u7740\u7406\u89e3\u4e0d\u540c\u7684\u51fd\u6570\u3002\n <\/div>\n

\u53c2\u6570\u5316\u51fd\u6570<\/h2>\n
\n \u53c2\u6570\u662f\u51fd\u6570\u5b9a\u4e49\u4e2d\u5217\u51fa\u7684\u540d\u79f0\u3002\u5b83\u4eec\u662f\u5c06\u503c\u4f20\u9012\u7ed9\u51fd\u6570\u7684\u673a\u5236\u3002\n <\/div>\n
\n \u53c2\u6570\u7684\u503c\u5728\u8c03\u7528\u671f\u95f4\u4f20\u9012\u7ed9\u51fd\u6570\u3002\u9664\u975e\u660e\u786e\u6307\u5b9a\uff0c\u5426\u5219\u4f20\u9012\u7ed9\u51fd\u6570\u7684\u503c\u7684\u6570\u91cf\u5fc5\u987b\u4e0e\u5b9a\u4e49\u7684\u53c2\u6570\u6570\u91cf\u76f8\u5339\u914d\u3002\n <\/div>\n
\n \u8bed\u6cd5\n <\/div>\n
\n \u5b9a\u4e49\u53c2\u6570\u5316\u51fd\u6570\u7684\u8bed\u6cd5\u662f:\n <\/div>\n
\n
function function_name( parameter1,parameter2 ,&#x2026;..parameterN) {   <\/span>\n     \/\/body of the function<\/span>\n}<\/pre>\n

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

\n \u4f8b\u5b50\n <\/div>\n
\n \u5728\u8fd9\u4e2a\u53c2\u6570\u5316\u51fd\u6570\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u51fd\u6570mul()\uff0c\u5b83\u63a5\u53d7\u4e24\u4e2a\u53c2\u6570 x \u548c y, \u5e76\u5728\u7ed3\u679c\u4e2d\u8fd4\u56de\u5b83\u4eec\u7684\u4e58\u6cd5\u3002\u53c2\u6570\u503c\u5728\u8c03\u7528\u671f\u95f4\u4f20\u9012\u7ed9\u51fd\u6570\u3002\n <\/div>\n
\n
function mul( x , y) { \n    var <\/span>c = x * y;\n    console.log<\/span>(\"x = \"<\/span>+x);\n    console.log<\/span>(\"y = \"<\/span>+y);\n    console.log<\/span>(\"x * y = \"<\/span>+c); \n } \nmul(20,30);<\/pre>\n

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

\n \u8f93\u51fa\n <\/div>\n
\n
x = 20\ny = 30\nx * y = 600<\/pre>\n

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

\u9ed8\u8ba4\u51fd\u6570\u53c2\u6570<\/h2>\n
\n \u5728 ES6 \u4e2d\uff0c\u5982\u679c\u53c2\u6570\u672a\u5b9a\u4e49\u6216\u6ca1\u6709\u4f20\u9012\u503c\uff0c\u8be5\u51fd\u6570\u5141\u8bb8\u4f7f\u7528\u9ed8\u8ba4\u503c\u521d\u59cb\u5316\u53c2\u6570\u3002\n <\/div>\n
\n \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
\n
function show (num1, num2=200) \n{\n    console.log<\/span>(\"num1 = \"<\/span> +num1);\n    console.log<\/span>(\"num2 = \"<\/span> +num2);\n}\nshow(100);<\/pre>\n

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

\n \u5728\u4e0a\u8ff0\u51fd\u6570\u4e2d\uff0cnum2\u7684\u503c\u9ed8\u8ba4\u8bbe\u7f6e\u4e3a200\u3002\u5982\u679c\u6ca1\u6709\u663e\u5f0f\u4f20\u9012 num2 \u7684\u503c\uff0c\u8be5\u51fd\u6570\u5c06\u59cb\u7ec8\u5c06 200 \u89c6\u4e3a num2 \u7684\u503c\u3002\n <\/div>\n
\n \u8f93\u51fa\n <\/div>\n
\n
100 200<\/pre>\n

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

\n \u5982\u679c\u51fd\u6570\u663e\u5f0f\u4f20\u9012\u5176\u503c\uff0c\u53c2\u6570'num2'\u7684\u9ed8\u8ba4\u503c\u5c06\u88ab\u8986\u76d6\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7\u4e0b\u9762\u7684\u4f8b\u5b50\u6765\u7406\u89e3:\n <\/div>\n
\n \u4f8b\u5982\n <\/div>\n
\n
function show(num1, num2=200) \n{\n    console.log<\/span>(\"num1 = \"<\/span> +num1);\n    console.log<\/span>(\"num2 = \"<\/span> +num2);\n}\nshow(100,500);<\/pre>\n

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

\n \u8f93\u51fa\n <\/div>\n
\n
100 500<\/pre>\n

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

Rest \u53c2\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\uff0crest \u53c2\u6570\u5145\u5f53\u540c\u4e00\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\u7684\u540d\u79f0\u5e94\u8be5\u4ee5 \u6269\u5c55\u8fd0\u7b97\u7b26\u5177\u6709\u4e09\u4e2a\u53e5\u70b9(\u4e0d\u8d85\u8fc7\u4e09\u4e2a\u6216\u4e0d\u5c0f\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
\n
function show(a, ...args)\n{\n    console.log<\/span>(a + \" \"<\/span> + args);\n}\nshow(50,60,70,80,90,100);<\/pre>\n

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

\n \u8f93\u51fa\n <\/div>\n
\n
50,60,70,80,90,100<\/pre>\n

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

\n \u6ce8\u610f :<\/span>\u5176\u4f59\u53c2\u6570\u5e94\u8be5\u5728\u51fd\u6570\u53c2\u6570\u5217\u8868\u7684\u6700\u540e\u3002\n <\/div>\n

\u8fd4\u56de\u51fd\u6570<\/h2>\n
\n \u51fd\u6570\u4e5f\u8fd4\u56de\u4f7f\u7528 return \u8bed\u53e5\u5c06\u503c\u4f20\u9012\u7ed9\u8c03\u7528\u8005\u3002\u8fd9\u4e9b\u51fd\u6570\u79f0\u4e3a\u8fd4\u56de\u51fd\u6570\u3002\u8fd4\u56de\u51fd\u6570\u5e94\u59cb\u7ec8\u4ee5 return \u8bed\u53e5\u7ed3\u675f\u3002\u4e00\u4e2a\u51fd\u6570\u4e2d\u53ea\u80fd\u6709\u4e00\u4e2areturn\u8bed\u53e5\uff0c\u5e76\u4e14return\u8bed\u53e5\u5e94\u8be5\u662f\u51fd\u6570\u4e2d\u7684\u6700\u540e\u4e00\u4e2a\u8bed\u53e5\u3002\n <\/div>\n
\n \u5f53JavaScript\u5230\u8fbereturn \u8bed\u53e5\uff0c\u51fd\u6570\u505c\u6b62\u6267\u884c\u5e76\u7acb\u5373\u9000\u51fa\u3002\u8fd9\u5c31\u662f\u4e3a\u4ec0\u4e48\u60a8\u53ef\u4ee5\u4f7f\u7528 return \u8bed\u53e5\u7acb\u5373\u505c\u6b62\u51fd\u6570\u7684\u6267\u884c\u3002\n <\/div>\n
\n \u8bed\u6cd5\n <\/div>\n
\n \u51fd\u6570\u53ef\u4ee5\u4f7f\u7528 return \u8bed\u53e5\u8fd4\u56de\u503c\uff0c\u540e\u8ddf\u4e00\u4e2a\u503c\u6216\u4e00\u4e2a\u8868\u8fbe\u5f0f\u3002\u8fd4\u56de\u51fd\u6570\u7684\u8bed\u6cd5\u5982\u4e0b:\n <\/div>\n
\n
function function_name() { \n    \/\/code to be executed<\/span>\n    return <\/span>value; \n}<\/pre>\n

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

\n \u793a\u4f8b\n <\/div>\n
\n
function add( a, b )\n{\n    return <\/span>a+b;\n}\nvar <\/span>sum = add(10,20);\nconsole.log<\/span>(sum);<\/pre>\n

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

\n \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u51fd\u6570add()\uff0c\u5b83\u6709\u4e24\u4e2a\u53c2\u6570a \u548cb\u3002\u6b64\u51fd\u6570\u5c06\u6dfb\u52a0\u7684\u53c2\u6570\u8fd4\u56de\u7ed9\u8c03\u7528\u8005\u3002\n <\/div>\n
\n \u6267\u884c\u4e0a\u8ff0\u4ee3\u7801\u540e\uff0c\u60a8\u5c06\u83b7\u5f97\u4ee5\u4e0b\u8f93\u51fa\u3002\n <\/div>\n
\n \u8f93\u51fa\n <\/div>\n
\n
30<\/pre>\n

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

\u751f\u6210\u5668\u51fd\u6570<\/h2>\n
\n \u751f\u6210\u5668(\u6216\u751f\u6210\u5668\u51fd\u6570)\u662f ES6 \u4e2d\u5f15\u5165\u7684\u65b0\u6982\u5ff5\u3002\u5b83\u4e3a\u60a8\u63d0\u4f9b\u4e86\u4e00\u79cd\u4f7f\u7528\u8fed\u4ee3\u5668\u548c\u51fd\u6570\u7684\u65b0\u65b9\u6cd5\u3002\n <\/div>\n
\n ES6 \u751f\u6210\u5668\u662f\u4e00\u79cd\u4e0d\u540c\u7684\u51fd\u6570\uff0c\u53ef\u4ee5\u5728\u4e2d\u95f4\u6682\u505c\u4e00\u6b21\u6216\u591a\u6b21\uff0c\u7a0d\u540e\u53ef\u4ee5\u6062\u590d\u3002\n <\/div>\n
\n \u60a8\u53ef\u4ee5\u901a\u8fc7\u5355\u51fb\u6b64\u94fe\u63a5ES6 Generators\u4e86\u89e3\u6709\u5173\u751f\u6210\u5668\u51fd\u6570\u7684\u66f4\u591a\u4fe1\u606f\u3002\n <\/div>\n

\u533f\u540d\u51fd\u6570<\/h2>\n
\n \u533f\u540d\u51fd\u6570\u53ef\u4ee5\u5b9a\u4e49\u4e3a\u6ca1\u6709\u540d\u5b57\u7684\u51fd\u6570\u3002\u533f\u540d\u51fd\u6570\u4e0d\u7ed1\u5b9a\u6807\u8bc6\u7b26\u3002\u5b83\u53ef\u4ee5\u5728\u8fd0\u884c\u65f6\u52a8\u6001\u58f0\u660e\u3002\u533f\u540d\u51fd\u6570\u5728\u521d\u59cb\u521b\u5efa\u540e\u4e0d\u53ef\u8bbf\u95ee\u3002\n <\/div>\n
\n \u533f\u540d\u51fd\u6570\u53ef\u4ee5\u5728\u53d8\u91cf\u4e2d\u8d4b\u503c\u3002\u8fd9\u79cd\u8868\u8fbe\u5f0f\u88ab\u79f0\u4e3a\u51fd\u6570\u8868\u8fbe\u5f0f\u3002\u533f\u540d\u51fd\u6570\u7684\u8bed\u6cd5\u5982\u4e0b\u3002\n <\/div>\n
\n \u8bed\u6cd5\n <\/div>\n
\n
var <\/span>y = function( [arguments] ) \n{ \n     \/\/code to be executed<\/span>\n}<\/pre>\n

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

\n \u793a\u4f8b\n <\/div>\n
\n
var <\/span>hello = function() {\n   console.log<\/span>('Hello World'<\/span>);  \n   console.log<\/span>('I am an anonymous function'<\/span>); \n }\n hello();<\/pre>\n

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

\n \u8f93\u51fa\n <\/div>\n
\n
Hello World\nI am an anonymous function<\/pre>\n

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

\u533f\u540d\u51fd\u6570\u4f5c\u4e3a\u53c2\u6570<\/h3>\n
\n \u533f\u540d\u51fd\u6570\u7684\u4e00\u4e2a\u5e38\u89c1\u7528\u9014\u662f\u5b83\u53ef\u4ee5\u7528\u4f5c\u5176\u4ed6\u51fd\u6570\u7684\u53c2\u6570\u3002\n <\/div>\n
\n \u793a\u4f8b\n <\/div>\n
\n \u7528\u4f5c\u5176\u4ed6\u51fd\u6570\u7684\u53c2\u6570\u3002\n <\/div>\n
\n
setTimeout(function() \n{\n   console.log<\/span>('Hello World'<\/span>);\n }, 2000);<\/pre>\n

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

\n \u5f53\u4f60\u6267\u884c\u4e0a\u9762\u7684\u4ee3\u7801\u65f6\uff0c\u5b83\u4f1a\u5728\u4e24\u79d2\u540e\u663e\u793a\u8f93\u51fa\u3002\n <\/div>\n
\n \u8f93\u51fa\n <\/div>\n
\n
Hello World<\/pre>\n

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

\u53c2\u6570\u5316\u533f\u540d\u51fd\u6570<\/h3>\n
\n \u793a\u4f8b\n <\/div>\n
\n
var <\/span>anon = function(a,b)\n{ \n   return <\/span>a+b \n}\nfunction sum() { \n   var <\/span>res; \n   res = anon(100,200); \n   console.log<\/span>(\"Sum: \"<\/span>+res)  \n} \nsum()<\/pre>\n

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

\n \u8f93\u51fa\n <\/div>\n
\n
Sum: 300<\/pre>\n

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

\u7bad\u5934\u51fd\u6570<\/h2>\n
\n \u7bad\u5934\u51fd\u6570\u662f\u5728 ES6 \u4e2d\u5f15\u5165\u7684\uff0c\u5b83\u4e3a\u60a8\u63d0\u4f9b\u4e86\u4e00\u79cd\u66f4\u51c6\u786e\u7684 JavaScript \u7f16\u5199\u51fd\u6570\u7684\u65b9\u5f0f\u3002\u5b83\u4eec\u5141\u8bb8\u6211\u4eec\u7f16\u5199\u66f4\u5c0f\u7684\u51fd\u6570\u8bed\u6cd5\u3002\u7bad\u5934\u51fd\u6570\u4f7f\u60a8\u7684\u4ee3\u7801\u66f4\u5177\u53ef\u8bfb\u6027\u548c\u7ed3\u6784\u5316\u3002\n <\/div>\n
\n \u60a8\u53ef\u4ee5\u901a\u8fc7\u5355\u51fb\u6b64\u94fe\u63a5\u7684\u66f4\u591a\u4fe1\u606f\u3002 com\/es6-arrow-function\">ES6 \u7bad\u5934\u51fd\u6570.\n <\/div>\n

\u51fd\u6570\u63d0\u5347<\/h2>\n
\n \u4f5c\u4e3a\u53d8\u91cf\u63d0\u5347\uff0c\u6211\u4eec\u53ef\u4ee5\u5728\u51fd\u6570\u4e2d\u8fdb\u884c\u63d0\u5347\u3002\u4e0e\u53d8\u91cf\u63d0\u5347\u4e0d\u540c\uff0c\u5f53\u51fd\u6570\u58f0\u660e\u88ab\u63d0\u5347\u65f6\uff0c\u5b83\u53ea\u63d0\u5347\u51fd\u6570\u5b9a\u4e49\u800c\u4e0d\u662f\u4ec5\u4ec5\u63d0\u5347\u51fd\u6570\u7684\u540d\u79f0\u3002\n <\/div>\n
\n \u8ba9\u6211\u4eec\u5c1d\u8bd5\u901a\u8fc7\u4f7f\u7528\u4ee5\u4e0b\u793a\u4f8b\u6765\u8bf4\u660e JavaScript \u4e2d\u7684\u51fd\u6570\u63d0\u5347:\n <\/div>\n
\n \u4f8b\u5b50\n <\/div>\n
\n \u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5728\u7f16\u5199\u51fd\u6570\u4e4b\u524d\u8c03\u7528\u5b83\u3002\u8ba9\u6211\u4eec\u770b\u770b\u5728\u7f16\u5199\u51fd\u6570\u4e4b\u524d\u8c03\u7528\u51fd\u6570\u4f1a\u53d1\u751f\u4ec0\u4e48\u3002\n <\/div>\n
\n
hello();\nfunction hello() {\n  console.log<\/span>(\"Hello world \"<\/span>);\n}<\/pre>\n

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

\n \u8f93\u51fa\n <\/div>\n
\n
Hello world<\/pre>\n

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

\n \u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u5728\u7f16\u5199\u4e4b\u524d\u5148\u8c03\u7528\u4e86\u51fd\u6570\uff0c\u4f46\u4ee3\u7801\u4ecd\u7136\u6709\u6548\u3002\n <\/div>\n
\n \u4f46\u662f\uff0c\u51fd\u6570\u8868\u8fbe\u5f0f\u4e0d\u80fd\u88ab\u63d0\u5347\u3002\u8ba9\u6211\u4eec\u8bd5\u7740\u770b\u770b\u4e0b\u9762\u4f8b\u5b50\u4e2d\u63d0\u5347\u51fd\u6570\u8868\u8fbe\u5f0f\u7684\u8bf4\u660e\u3002\n <\/div>\n
\n
hello();\nvar <\/span>hello = function() {\n  console.log<\/span>(\"Hello world \"<\/span>);\n}<\/pre>\n

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

\n \u5f53\u4f60\u6267\u884c\u4e0a\u9762\u7684\u4ee3\u7801\u65f6\uff0c\u4f60\u4f1a\u5f97\u5230\u4e00\u4e2a\"TypeError: hello is not a function.\"\u8fd9\u662f\u56e0\u4e3a\u51fd\u6570\u8868\u8fbe\u5f0f\u4e0d\u80fd\u88ab\u63d0\u5347\u3002\n <\/div>\n
\n \u8f93\u51fa\n <\/div>\n
\n
TypeError: hello is not a function<\/pre>\n

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

\u51fd\u6570\u6784\u9020\u5668<\/h2>\n
\n \u662f\u5b9a\u4e49\u65b0\u51fd\u6570\u7684\u65b9\u5f0f\u3002 function \u8bed\u53e5\u4e0d\u662f\u5b9a\u4e49\u65b0\u51fd\u6570\u7684\u552f\u4e00\u65b9\u6cd5\uff1b\u6211\u4eec\u8fd8\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528 Function() \u6784\u9020\u51fd\u6570 \u548c new \u8fd0\u7b97\u7b26\u6765\u52a8\u6001\u5b9a\u4e49\u51fd\u6570\u3002\n <\/div>\n
\n \u5b83\u6bd4\u4f7f\u7528\u58f0\u660e\u51fd\u6570\u6548\u7387\u4f4e\u51fd\u6570\u8868\u8fbe\u5f0f\u6216\u51fd\u6570\u8bed\u53e5\u3002\n <\/div>\n
\n \u8bed\u6cd5\n <\/div>\n
\n \u8bed\u6cd5\u4f7f\u7528 Function() \u6784\u9020\u51fd\u6570\u521b\u5efa\u51fd\u6570\u3002\n <\/div>\n
\n
var <\/span>variable_name = new <\/span>Function(arg1, arg2..., \"Body of the Function\"<\/span>);<\/pre>\n

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

\n \u4f7f\u7528\u7684\u53c2\u6570\n <\/div>\n
\n \u5b83\u7684\u8bed\u6cd5\u5305\u62ec:\n <\/div>\n
\n arg1, arg2, ... argN:\u8fd9\u4e9b\u662f\u51fd\u6570\u7528\u4f5c\u5f62\u5f0f\u53c2\u6570\u540d\u79f0\u7684\u540d\u79f0\u3002\u6bcf\u4e2a\u53c2\u6570\u5fc5\u987b\u662f\u4e00\u4e2a\u4e0e JavaScript \u7684\u6709\u6548\u6807\u8bc6\u7b26\u76f8\u5bf9\u5e94\u7684\u5b57\u7b26\u4e32\u3002\n <\/div>\n
\n \u51fd\u6570\u4f53: \u5b83\u662f\u4e00\u4e2a\u5305\u542b JavaScript \u8bed\u53e5 \u5305\u542b\u51fd\u6570\u5b9a\u4e49\u3002\n <\/div>\n
\n \u5b83\u53ef\u4ee5\u5305\u542b\u4efb\u610f\u6570\u91cf\u7684\u5b57\u7b26\u4e32\u53c2\u6570\u3002\u6700\u540e\u4e00\u4e2a\u53c2\u6570\u662f\u51fd\u6570\u4f53\uff0c\u5b83\u53ef\u4ee5\u5305\u542b JavaScript \u7684\u4efb\u610f\u8bed\u53e5\uff0c\u5b83\u4eec\u4e4b\u95f4\u7528\u5206\u53f7\u5206\u9694\u3002\n <\/div>\n
\n function() \u6784\u9020\u51fd\u6570\u4e0d\u4f20\u9012\u4efb\u4f55\u4e3a\u51fd\u6570\u6307\u5b9a\u540d\u79f0\u7684\u53c2\u6570\u5b83\u521b\u5efa\u7684\u51fd\u6570\u3002\n <\/div>\n
\n \u793a\u4f8b\n <\/div>\n
\n
\/\/ creating a function that takes two arguments and returns the product of both arguments<\/span>\nvar <\/span>mul = new <\/span>Function('a'<\/span>, 'b'<\/span>, 'return <\/span>a * b'<\/span>);\n\/\/ calling of function<\/span>\nconsole.log<\/span>(\"The product of the numbers is: \"<\/span> +mul(5, 5));<\/pre>\n

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

\n \u8f93\u51fa\n <\/div>\n
\n
The product of the numbers is: 25<\/pre>\n

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

\u7acb\u5373\u8c03\u7528\u51fd\u6570\u8868\u8fbe\u5f0f(IIFE)<\/h2>\n
\n \u5b83\u662f JavaScript \u4e2d\u7684\u4e00\u4e2a\u51fd\u6570\uff0c\u4e00\u5b9a\u4e49\u5c31\u8fd0\u884c\u3002 IIFE(\u7acb\u5373\u8c03\u7528\u51fd\u6570\u8868\u8fbe\u5f0f)\u53ef\u7528\u4e8e\u907f\u514d\u5757\u5185\u7684\u53d8\u91cf\u63d0\u5347\u3002\u5b83\u5141\u8bb8\u516c\u5f00\u8bbf\u95ee\u65b9\u6cd5\uff0c\u540c\u65f6\u4fdd\u7559\u51fd\u6570\u4e2d\u5b9a\u4e49\u7684\u53d8\u91cf\u7684\u9690\u79c1\u3002\n <\/div>\n
\n \u60a8\u53ef\u4ee5\u901a\u8fc7\u5355\u51fb\u6b64\u94fe\u63a5\u4e86\u89e3\u6709\u5173 IIFE \u7684\u66f4\u591a\u4fe1\u606f \u7acb\u5373\u8c03\u7528\u51fd\u6570\u8868\u8fbe\u5f0f(IIFE)\n <\/div>\n

JavaScript \u51fd\u6570\u548c\u9012\u5f52<\/h2>\n
\n \u5f53\u4e00\u4e2a\u51fd\u6570\u8c03\u7528\u81ea\u8eab\uff0c\u90a3\u4e48\u5b83\u88ab\u79f0\u4e3a\u9012\u5f52\u51fd\u6570\u3002\u9012\u5f52\u662f\u4e00\u79cd\u901a\u8fc7\u8ba9\u51fd\u6570\u91cd\u590d\u8c03\u7528\u81ea\u8eab\u76f4\u5230\u8fbe\u5230\u7ed3\u679c\u6765\u8fed\u4ee3\u64cd\u4f5c\u7684\u6280\u672f\u3002\n <\/div>\n
\n \u5b83\u662f\u5f53\u6211\u4eec\u9700\u8981\u5728\u5faa\u73af\u4e2d\u4f7f\u7528\u4e0d\u540c\u53c2\u6570\u5b9a\u671f\u8c03\u7528\u76f8\u540c\u51fd\u6570\u65f6\u7684\u6700\u4f73\u65b9\u6cd5\u3002\n <\/div>\n
\n \u793a\u4f8b\n <\/div>\n
\n
function fact(num) {\n      if <\/span>(num <= 1) {\n          return <\/span>1;\n      } else <\/span>{\n          return <\/span>num * fact(num-1);\n      }\n  } \n  console.log<\/span>(fact(6));\n  console.log<\/span>(fact(5));\n  console.log<\/span>(fact(4));<\/pre>\n

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

\n \u8f93\u51fa\n <\/div>\n
\n
720\n120\n24<\/pre>\n

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

\u51fd\u6570\u8868\u8fbe\u5f0fv\/s\u51fd\u6570\u58f0\u660e<\/h2>\n
\n \u4e24\u8005\u7684\u6839\u672c\u533a\u522b\u5728\u4e8e\u51fd\u6570\u58f0\u660e\u5728\u6267\u884c\u4e4b\u524d\u88ab\u89e3\u6790\uff0c\u800c\u51fd\u6570\u8868\u8fbe\u5f0f\u53ea\u6709\u5728\u6267\u884c\u65f6\u624d\u88ab\u89e3\u6790\u811a\u672c\u5f15\u64ce\u5728\u6267\u884c\u8fc7\u7a0b\u4e2d\u9047\u5230\u5b83\u3002\n <\/div>\n
\n \u4e0e\u51fd\u6570\u58f0\u660e\u4e0d\u540c\uff0cJavaScript \u4e2d\u7684\u51fd\u6570\u8868\u8fbe\u5f0f\u4e0d\u4f1a\u63d0\u5347\u3002\u51fd\u6570\u8868\u8fbe\u5f0f\u5728\u5b9a\u4e49\u4e4b\u524d\u4e0d\u80fd\u4f7f\u7528\u3002\n <\/div>\n
\n \u51fd\u6570\u58f0\u660e\u548c\u51fd\u6570\u8868\u8fbe\u5f0f\u7684\u4e3b\u8981\u533a\u522b\u5728\u4e8e\u51fd\u6570\u540d\uff0c\u5728\u521b\u5efa\u533f\u540d\u51fd\u6570\u7684\u51fd\u6570\u8868\u8fbe\u5f0f\u4e2d\u53ef\u4ee5\u7701\u7565\u3002\n <\/div>\n

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