{"id":1911,"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 \u6761\u4ef6\u8bed\u53e5","status":"publish","type":"post","link":"https:\/\/bianchenghao6.com\/1911.html","title":{"rendered":"ES6 \u6761\u4ef6\u8bed\u53e5"},"content":{"rendered":"


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

\n

ES6 \u6761\u4ef6\u8bed\u53e5<\/h1>\n<\/p><\/div>\n

ES6 \u6761\u4ef6\u8bed\u53e5<\/h2>\n
\n ES6 \u6761\u4ef6\u8bed\u53e5\u7528\u4e8e\u6267\u884c\u4e0d\u540c\u7684\u64cd\u4f5c\u57fa\u4e8e\u5404\u79cd\u6761\u4ef6\u3002\u6761\u4ef6\u8bed\u53e5\u5728\u6267\u884c\u6307\u4ee4\u4e4b\u524d\u8bc4\u4f30\u6761\u4ef6\u3002\n <\/div>\n
\n \u5728\u7f16\u5199\u4ee3\u7801\u65f6\uff0c\u60a8\u9700\u8981\u9488\u5bf9\u4e0d\u540c\u7684\u51b3\u7b56\u6267\u884c\u4e0d\u540c\u7684\u64cd\u4f5c\u3002\u60a8\u53ef\u4ee5\u4f7f\u7528\u6761\u4ef6\u8bed\u53e5\u8f7b\u677e\u5730\u6267\u884c\u5b83\u3002\n <\/div>\n

ES6 \u6761\u4ef6\u8bed\u53e5_https:\/\/bianchenghao6.com_\u3010ES6 \u6559\u7a0b\u3011_\u7b2c1\u5f20 <\/p>\n

\u6761\u4ef6\u8bed\u53e5\u7684\u7c7b\u578b<\/h2>\n
\n JavaScript \u4e2d\u7684\u6761\u4ef6\u8bed\u53e5\u5982\u4e0b:\n <\/div>\n

if \u8bed\u53e5<\/span>
\n if....else \u8bed\u53e5<\/span>
\n if....else if....statement<\/span>
\n \u5d4c\u5957 if \u8bed\u53e5<\/span>
\n switch \u8bed\u53e5<\/span> <\/p>\n

\n \u8ba9\u6211\u4eec\u8bd5\u7740\u8be6\u7ec6\u8bf4\u660e\u8fd9\u4e9b\u6761\u4ef6\u8bed\u53e5\u3002\n <\/div>\n

if \u8bed\u53e5<\/h3>\n
\n \u8fd9\u662f\u6700\u7b80\u5355\u7684\u51b3\u7b56\u8bed\u53e5\u4e4b\u4e00\u7528\u4e8e\u51b3\u5b9a\u5728\u67d0\u4e2a\u6761\u4ef6\u4e3a\u771f\u65f6\u662f\u5426\u6267\u884c\u4e00\u6bb5 JavaScript \u4ee3\u7801\u3002\n <\/div>\n
\n \u8bed\u6cd5\n <\/div>\n
\n
if <\/span>(condition) {\n  \/\/ block of code will execute if <\/span>the condition is true<\/span><\/span><\/span>\n}<\/pre>\n

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

\n \u5982\u679c\u6761\u4ef6\u4e3a\u771f\uff0cif \u8bed\u53e5\u4e2d\u7684\u4ee3\u7801\u5c06\u6267\u884c\uff0c\u4f46\u5982\u679c\u6761\u4ef6\u4e3a\u5047\uff0c\u5219if \u8bed\u53e5\u7ed3\u675f\u540e\u7684\u4ee3\u7801(\u7ed3\u675f\u540e)\u82b1\u62ec\u53f7) \u5c06\u6267\u884c\u3002\n <\/div>\n

\u6ce8\u610f: if \u8bed\u53e5\u5fc5\u987b\u4ee5\u5c0f\u5199\u5b57\u6bcd\u4e66\u5199\u3002\u4f7f\u7528\u5927\u5199\u5b57\u6bcd(If \u6216 IF)\u4f1a\u5bfc\u81f4 JavaScript \u9519\u8bef\u3002 <\/p>\n

\n \u6d41\u7a0b\u56fe\n <\/div>\n

ES6 \u6761\u4ef6\u8bed\u53e5_https:\/\/bianchenghao6.com_\u3010ES6 \u6559\u7a0b\u3011_\u7b2c2\u5f20 <\/p>\n

\n \u4f8b\u5982\n <\/div>\n
\n
var <\/span> x = 78;\nif <\/span>(x>70) { \n   console.log<\/span>(\"x is greater\"<\/span>) \n}<\/pre>\n

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

\n \u8f93\u51fa\n <\/div>\n
\n
x is greater<\/pre>\n

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

if....else \u8bed\u53e5<\/h3>\n
\n if....else \u8bed\u53e5 \u5305\u62ec\u4e24\u4e2a\u5757\uff0c\u5206\u522b\u662f if \u5757 \u548celse \u5757\u3002 \u8fd9\u662f\u63a7\u5236\u8bed\u53e5\u7684\u4e0b\u4e00\u79cd\u5f62\u5f0f\uff0c\u5b83\u5141\u8bb8\u4ee5\u66f4\u53ef\u63a7\u7684\u65b9\u5f0f\u6267\u884c JavaScript\u3002\u5f53\u60a8\u9700\u8981\u68c0\u67e5\u4e24\u4e2a\u4e0d\u540c\u7684\u6761\u4ef6\u5e76\u6267\u884c\u4e00\u7ec4\u4e0d\u540c\u7684\u4ee3\u7801\u65f6\u4f7f\u7528\u5b83\u3002 else \u8bed\u53e5\u7528\u4e8e\u6307\u5b9a\u5728\u6761\u4ef6\u4e3a\u5047\u65f6\u6267\u884c\u4ee3\u7801\u5757\u3002\n <\/div>\n
\n \u8bed\u6cd5\n <\/div>\n
\n
if <\/span>(condition)\n {\n  \/\/ block of code will execute if <\/span>the condition is true<\/span><\/span><\/span>\n}\n else\n {\n  \/\/ block of code will execute if <\/span>the condition is false<\/span><\/span>\n}<\/pre>\n

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

\n \u5982\u679c\u6761\u4ef6\u4e3a\u771f\uff0c\u5219if\u5757\u4e2d\u7684\u8bed\u53e5\u5c06\u88ab\u6267\u884c\uff0c\u4f46\u5982\u679c\u6761\u4ef6\u4e3a\u5047\uff0c\u5219else\u5757\u4e2d\u7684\u8bed\u53e5\u5c06\u88ab\u6267\u884c\n <\/div>\n
\n \u6d41\u7a0b\u56fe\n <\/div>\n

ES6 \u6761\u4ef6\u8bed\u53e5_https:\/\/bianchenghao6.com_\u3010ES6 \u6559\u7a0b\u3011_\u7b2c3\u5f20 <\/p>\n

\n \u4f8b\u5982\n <\/div>\n
\n \u8ba9\u6211\u4eec\u8bd5\u7740\u901a\u8fc7\u4e0b\u9762\u7684\u4f8b\u5b50\u6765\u7406\u89e3if....else\u8bed\u53e5:\n <\/div>\n
\n
var <\/span>x = 40, y=20; \nif <\/span>(x < y) \n{ \n   console.log<\/span>(\"y is greater\"<\/span>); \n} \nelse <\/span>\n{ \n   console.log<\/span>(\"x is greater\"<\/span>); \n}<\/pre>\n

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

\n \u8f93\u51fa\n <\/div>\n
\n
x is greater<\/pre>\n

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

if....else if.....else \u8bed\u53e5<\/h3>\n
\n \u7528\u4e8e\u6d4b\u8bd5\u591a\u4e2a\u6761\u4ef6\u3002 if \u8bed\u53e5\u53ef\u4ee5\u6709\u591a\u4e2a\u6216\u96f6\u4e2a else if \u8bed\u53e5\uff0c\u5e76\u4e14\u5fc5\u987b\u5728\u4f7f\u7528 else \u8bed\u53e5\u4e4b\u524d\u4f7f\u7528\u5b83\u4eec\u3002\u60a8\u5e94\u8be5\u59cb\u7ec8\u7262\u8bb0else \u8bed\u53e5 \u5fc5\u987b\u5728 else if \u8bed\u53e5\u4e4b\u540e\u3002\n <\/div>\n
\n \u8bed\u6cd5\n <\/div>\n
\n
if <\/span>(condition1) \n{\n  \/\/  block of code will execute if <\/span>condition1 is true<\/span><\/span><\/span>\n}\n else <\/span>if <\/span>(condition2) \n{\n  \/\/  block of code will execute if <\/span>the condition1 is false<\/span> <\/span>and condition2 is true<\/span><\/span><\/span>\n} \nelse <\/span>\n{\n  \/\/  block of code will execute if <\/span>the condition1 is false<\/span> <\/span>and condition2 is false<\/span><\/span>\n}<\/pre>\n

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

\n \u793a\u4f8b\n <\/div>\n
\n
var <\/span>a = 10, b = 20, c = 30; \nif( a > b && a > c) { \n   console.log<\/span>(\"a is greater\"<\/span>); \n} else <\/span>if( b > a && b > c ) { \n    console.log<\/span>(\"b is greater\"<\/span>); \n} else <\/span>{ \n    console.log<\/span>(\"c is greater\"<\/span>); \n}<\/pre>\n

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

\n \u8f93\u51fa\n <\/div>\n
\n
c is greater<\/pre>\n

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

\u5d4c\u5957\u7684 if \u8bed\u53e5<\/h3>\n
\n \u5b83\u662f if \u8bed\u53e5\u4e2d\u7684 if \u8bed\u53e5\u3002\n <\/div>\n
\n \u8bed\u6cd5\n <\/div>\n
\n
if <\/span>(condition1) \n{ \nStatement 1; \/\/It will execute when condition1 is true<\/span><\/span><\/span>\nif <\/span>(condition2)\n { \nStatement 2; \/\/It will execute when condition2 is true<\/span><\/span><\/span>\n}\nelse\n{\n Statement 3; \/\/It will execute when condition2 is false<\/span><\/span>\n}\n}<\/pre>\n

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

\n \u793a\u4f8b\n <\/div>\n
\n
var <\/span>num = 20;\nif <\/span>(num > 10)\n{\nif <\/span>(num%2==0)\nconsole.log<\/span>( num+ \" is greater than 10 and even number\"<\/span>);\nelse\nconsole.log<\/span>(num+ \" is greater than 10 and odd number\"<\/span>);\n}\nelse\n{\nconsole.log<\/span>(num+\" is smaller than 10\"<\/span>);\n}\nconsole.log<\/span>(\"After nested if <\/span>statement\"<\/span>);<\/pre>\n

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

\n \u8f93\u51fa\n <\/div>\n
\n
20 is greater than 10 and even number\nAfter nested if <\/span>statement<\/pre>\n

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

switch \u8bed\u53e5<\/h3>\n
\n \u5b83\u662f\u4e00\u4e2a\u591a\u8def\u5206\u652f\u8bed\u53e5\uff0c\u4e5f\u7528\u4e8e\u51b3\u7b56\u76ee\u7684\u3002\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0cswitch \u8bed\u53e5 \u6bd4 if-else \u8bed\u53e5\u66f4\u65b9\u4fbf\u3002\u5b83\u4e3b\u8981\u7528\u4e8e\u6240\u6709\u5206\u652f\u90fd\u4f9d\u8d56\u4e8e\u5355\u4e2a\u53d8\u91cf\u7684\u503c\u7684\u60c5\u51b5\u3002\u5b83\u6839\u636e\u4e0d\u540c\u7684\u60c5\u51b5\u6267\u884c\u4e00\u6bb5\u4ee3\u7801\u3002\n <\/div>\n
\n switch \u8bed\u53e5 \u4f7f\u7528 break \u6216 default \u5173\u952e\u5b57\uff0c\u4f46\u5b83\u4eec\u90fd\u662f\u53ef\u9009\u7684\u3002\u8ba9\u6211\u4eec\u5b9a\u4e49\u8fd9\u4e24\u4e2a\u5173\u952e\u5b57:\n <\/div>\n
\n break: \u5b83\u5728 switch \u8bed\u53e5 \u4e2d\u7528\u4e8e\u7ec8\u6b62\u8bed\u53e5\u7684\u5e8f\u5217\u3002\u4f7f\u7528\u662f\u53ef\u9009\u7684\u3002\u5982\u679c\u5b83\u88ab\u7701\u7565\uff0c\u90a3\u4e48\u6bcf\u4e2a\u8bed\u53e5\u90fd\u4f1a\u7ee7\u7eed\u6267\u884c\u3002\u5f53\u5b83\u88ab\u4f7f\u7528\u65f6\uff0c\u5b83\u5c06\u505c\u6b62\u5757\u5185\u7684\u6267\u884c\u3002\n <\/div>\n
\n default: \u5b83\u6307\u5b9a\u4e00\u4e9b\u4ee3\u7801\u5728\u6ca1\u6709\u5927\u5c0f\u5199\u5339\u914d\u65f6\u8fd0\u884c\u3002\u5f00\u5173\u4e2d\u53ea\u80fd\u6709\u4e00\u4e2a\u9ed8\u8ba4\u5173\u952e\u5b57\u3002\u5b83\u4e5f\u662f\u53ef\u9009\u7684\uff0c\u4f46\u5efa\u8bae\u4f7f\u7528\u5b83\uff0c\u56e0\u4e3a\u5b83\u53ef\u4ee5\u5904\u7406\u610f\u5916\u60c5\u51b5\u3002\n <\/div>\n
\n \u5982\u679c\u4f20\u9012\u7ed9switch\u7684\u6761\u4ef6\u4e0ecase\u4e2d\u7684\u4efb\u4f55\u503c\u90fd\u4e0d\u5339\u914d\uff0c\u5219\u6267\u884cdefault\u4e0b\u7684\u8bed\u53e5\u3002\n <\/div>\n
\n \u51e0\u70b9\u8bb0\u4f4f\n <\/div>\n

\u4e00\u4e2a switch \u8868\u8fbe\u5f0f\u53ef\u4ee5\u6709\u4e00\u4e2a\u6216\u591a\u4e2a case \u503c\u3002<\/span>
\n break \u548c default \u5173\u952e\u5b57\u7684\u4f7f\u7528\u662f\u53ef\u9009\u7684\u3002<\/span>
\n case \u8bed\u53e5\u53ea\u80fd\u5305\u542b\u5e38\u91cf\u548c\u6587\u5b57\u3002\u5b83\u4e0d\u80fd\u662f\u8868\u8fbe\u5f0f\u6216\u53d8\u91cf\u3002<\/span>
\n \u9664\u975e\u60a8\u5728\u6bcf\u4e2a\u5757\u7684\u4ee3\u7801\u540e\u653e\u7f6e\u4e00\u4e2a\u4e2d\u65ad\uff0c\u5426\u5219\u6267\u884c\u5c06\u4e0d\u65ad\u6d41\u5165\u4e0b\u4e00\u4e2a\u5757\u3002<\/span>
\n \u6ca1\u6709\u5fc5\u8981\u6700\u540e\u5c06 default case \u653e\u5728 switch \u5757\u4e2d\u3002<\/span> <\/p>\n

\n \u8bed\u6cd5\n <\/div>\n
\n
switch(expression){    \ncase <\/span>value1:    \n \/\/code to be executed; <\/span><\/span>\n break<\/span>;  \/\/optional <\/span><\/span>\ncase <\/span>value2:    \n \/\/code to be executed;    \n break<\/span>;  \/\/optional  \n......    \n    \ndefault:     \n code to be executed if <\/span>all cases are not matched;    \n}   <\/pre>\n

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

\n \u6d41\u7a0b\u56fe\n <\/div>\n

ES6 \u6761\u4ef6\u8bed\u53e5_https:\/\/bianchenghao6.com_\u3010ES6 \u6559\u7a0b\u3011_\u7b2c4\u5f20 <\/p>\n

\n \u793a\u4f8b\n <\/div>\n
\n
var <\/span>num = 5; \nswitch(num) { \n    case <\/span>0 : { \n      console.log<\/span>(\"Sunday\"<\/span>); \n      break<\/span>; \n   } \n   case <\/span>1 : { \n    console.log<\/span>(\"Monday\"<\/span>); \n    break<\/span>; \n } \n   case <\/span>2 : { \n    console.log<\/span>(\"Tuesday\"<\/span>); \n    break<\/span>; \n }    \n   case <\/span>3 : { \n    console.log<\/span>(\"Wednesday\"<\/span>); \n    break<\/span>; \n }\n   case <\/span>4 : { \n    console.log<\/span>(\"Thursday\"<\/span>); \n    break<\/span>; \n } \n   case <\/span>5 : { \n    console.log<\/span>(\"Friday\"<\/span>); \n    break<\/span>; \n } \n   case <\/span>6 : { \n    console.log<\/span>(\"Saturday\"<\/span>); \n    break<\/span>; \n }    \n   default: { \n      console.log<\/span>(\"Invalid choice\"<\/span>); \n      break<\/span>;              \n   } \n}<\/pre>\n

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

\n \u8f93\u51fa\n <\/div>\n
\n
Friday<\/pre>\n

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

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