{"id":1649,"date":"2024-11-10T15:30:14","date_gmt":"2024-11-10T07:30:14","guid":{"rendered":"https:\/\/fwq.ai\/blog\/1649\/"},"modified":"2024-11-10T15:30:14","modified_gmt":"2024-11-10T07:30:14","slug":"javascript-%e4%b8%ad%e7%9a%84-return%ef%bc%9a%e4%b8%8d%e4%bb%85%e4%bb%85%e6%98%af%e8%bf%94%e5%9b%9e%e5%80%bc%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/1649\/","title":{"rendered":"JavaScript \u4e2d\u7684 return\uff1a\u4e0d\u4ec5\u4ec5\u662f\u8fd4\u56de\u503c\uff1f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173052524479603.jpg\" class=\"aligncenter\" title=\"JavaScript \u4e2d\u7684 return\uff1a\u4e0d\u4ec5\u4ec5\u662f\u8fd4\u56de\u503c\uff1f\u63d2\u56fe\" alt=\"JavaScript \u4e2d\u7684 return\uff1a\u4e0d\u4ec5\u4ec5\u662f\u8fd4\u56de\u503c\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>\u6df1\u5165\u6d45\u51fa\u8c08 return \u7684\u7528\u6cd5<\/strong><\/p>\n<p>javascript \u4e2d\uff0creturn \u5173\u952e\u5b57\u626e\u6f14\u7740\u5173\u952e\u89d2\u8272\uff0c\u7528\u4e8e\u7ec8\u6b62\u51fd\u6570\u6267\u884c\u5e76\u8fd4\u56de\u4e00\u4e2a\u503c\u3002\u4f46\u5b83\u7684\u7528\u6cd5\u5e76\u975e\u4ec5\u9650\u4e8e\u6b64\uff0c\u672c\u6587\u5c06\u6df1\u5165\u63a2\u8ba8 return \u7684\u591a\u65b9\u9762\u4f5c\u7528\u3002<\/p>\n<p><strong>\u57fa\u672c\u7528\u6cd5<\/strong><\/p>\n<p>\u6700\u57fa\u672c\u7684 return \u7528\u6cd5\u662f\u7ec8\u6b62\u51fd\u6570\u6267\u884c\u5e76\u8fd4\u56de\u4e00\u4e2a\u503c\u3002\u4f8b\u5982\uff1a<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<pre>function sum(a, b) {\n  return a + b;\n}\n\nconst result = sum(1, 2); \/\/ result \u4e3a 3<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u8be5\u51fd\u6570\u63a5\u6536\u4e24\u4e2a\u53c2\u6570 a \u548c b\uff0c\u8ba1\u7b97\u5b83\u4eec\u7684\u548c\u5e76\u8fd4\u56de\u7ed3\u679c\u3002\u5f53\u51fd\u6570\u6267\u884c\u5230 return \u8bed\u53e5\u65f6\uff0c\u5b83\u5c06\u505c\u6b62\u6267\u884c\u5e76\u8fd4\u56de 3\u3002<\/p>\n<p><strong>\u7ec8\u6b62<\/strong><\/p>\n<p>\u5728\u4e8b\u4ef6\u5904\u7406\u4e2d\uff0creturn false \u53ef\u7ec8\u6b62\u4e8b\u4ef6\u5192\u6ce1\uff0c\u5373\u963b\u6b62\u4e8b\u4ef6\u4f20\u64ad\u5230\u7236\u5143\u7d20\u6216\u6587\u6863\u3002\u8fd9\u662f\u5904\u7406\u4e8b\u4ef6\u7684\u4e00\u4e2a\u5e38\u7528\u6280\u672f\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre>const button = document.getelementbyid('my-button');\n\nbutton.addeventlistener('click', (e) =&gt; {\n  \/\/ \u963b\u6b62\u4e8b\u4ef6\u5192\u6ce1\n  return false;\n});<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5f53\u6309\u94ae\u88ab\u5355\u51fb\u65f6\uff0cclick \u4e8b\u4ef6\u5904\u7406\u51fd\u6570\u4f1a\u89e6\u53d1\u3002\u901a\u8fc7 return false\uff0c\u8be5\u51fd\u6570\u963b\u6b62\u8be5\u4e8b\u4ef6\u8fdb\u4e00\u6b65\u4f20\u64ad\u5230\u6587\u6863\u5176\u4ed6\u5143\u7d20\u3002<\/p>\n<p><strong>\u95ed\u5305\u4e2d\u4f7f\u7528 return<\/strong><\/p>\n<p>\u5728\u95ed\u5305\u4e2d\uff0creturn \u5173\u952e\u5b57\u53ef\u7528\u6765\u6355\u83b7\u51fd\u6570\u6267\u884c\u4e0a\u4e0b\u6587\u4e2d\u7684\u53d8\u91cf\u3002\u4f8b\u5982\uff1a<\/p>\n<pre>const createCounter = () =&gt; {\n  let counter = 0;\n\n  return () =&gt; {\n    return counter++;\n  };\n};\n\nconst counterFunction = createCounter();\nconsole.log(counterFunction()); \/\/ 0\nconsole.log(counterFunction()); \/\/ 1<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u8fd9\u91cc\uff0ccreatecounter \u51fd\u6570\u8fd4\u56de\u4e00\u4e2a\u7acb\u5373\u6267\u884c\u51fd\u6570\uff08iife\uff09\uff0c\u5b83\u6355\u83b7\u4e86 counter \u53d8\u91cf\u3002\u5373\u4f7f counterfunction \u5728\u5916\u90e8\u51fd\u6570\u7ed3\u675f\u540e\u4ecd\u88ab\u8c03\u7528\uff0c\u5b83\u4ecd\u7136\u53ef\u4ee5\u8bbf\u95ee\u5916\u90e8\u51fd\u6570\u7684\u53d8\u91cf counter\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fJavaScript \u4e2d\u7684 return\uff1a\u4e0d\u4ec5\u4ec5\u662f\u8fd4\u56de\u503c\uff1f\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6df1\u5165\u6d45\u51fa\u8c08 return \u7684\u7528\u6cd5 javascript \u4e2d\uff0creturn \u5173\u952e\u5b57\u626e\u6f14\u7740\u5173\u952e\u89d2\u8272\uff0c\u7528\u4e8e\u7ec8\u6b62\u51fd\u6570\u6267\u884c\u5e76\u8fd4\u56de\u4e00\u4e2a\u503c\u3002\u4f46\u5b83\u7684\u7528\u6cd5\u5e76\u975e\u4ec5\u9650\u4e8e\u6b64\uff0c\u672c\u6587\u5c06\u6df1\u5165\u63a2\u8ba8 return \u7684\u591a\u65b9\u9762\u4f5c\u7528\u3002 \u57fa\u672c\u7528\u6cd5 \u6700\u57fa\u672c\u7684 return \u7528\u6cd5\u662f\u7ec8\u6b62\u51fd\u6570\u6267\u884c\u5e76\u8fd4\u56de\u4e00\u4e2a\u503c\u3002\u4f8b\u5982\uff1a \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b function sum(a, b) { return a + b; } const result = sum(1, 2); \/\/ result \u4e3a 3 \u767b\u5f55\u540e\u590d\u5236 \u8be5\u51fd\u6570\u63a5\u6536\u4e24\u4e2a\u53c2\u6570 a \u548c b\uff0c\u8ba1\u7b97\u5b83\u4eec\u7684\u548c\u5e76\u8fd4\u56de\u7ed3\u679c\u3002\u5f53\u51fd\u6570\u6267\u884c\u5230 return \u8bed\u53e5\u65f6\uff0c\u5b83\u5c06\u505c\u6b62\u6267\u884c\u5e76\u8fd4\u56de 3\u3002 \u7ec8\u6b62 \u5728\u4e8b\u4ef6\u5904\u7406\u4e2d\uff0creturn false \u53ef\u7ec8\u6b62\u4e8b\u4ef6\u5192\u6ce1\uff0c\u5373\u963b\u6b62\u4e8b\u4ef6\u4f20\u64ad\u5230\u7236\u5143\u7d20\u6216\u6587\u6863\u3002\u8fd9\u662f\u5904\u7406\u4e8b\u4ef6\u7684\u4e00\u4e2a\u5e38\u7528\u6280\u672f\uff0c\u4f8b\u5982\uff1a const button = document.getelementbyid(&#8216;my-button&#8217;); button.addeventlistener(&#8216;click&#8217;, (e) =&gt; { \/\/ \u963b\u6b62\u4e8b\u4ef6\u5192\u6ce1 return false; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-1649","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/1649","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=1649"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/1649\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=1649"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=1649"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=1649"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}