{"id":3280,"date":"2024-11-10T12:25:31","date_gmt":"2024-11-10T04:25:31","guid":{"rendered":"https:\/\/fwq.ai\/blog\/3280\/"},"modified":"2024-11-10T12:25:31","modified_gmt":"2024-11-10T04:25:31","slug":"javascript-%e4%b8%ad%e7%9a%84%e5%87%bd%e6%95%b0%e5%ae%9a%e4%b9%89%ef%bc%9afunction-%e5%85%b3%e9%94%ae%e5%ad%97%e5%92%8c%e5%8c%bf%e5%90%8d%e5%87%bd%e6%95%b0%e8%a1%a8%e8%be%be%e5%bc%8f%ef%bc%8c%e5%93%aa","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/3280\/","title":{"rendered":"JavaScript \u4e2d\u7684\u51fd\u6570\u5b9a\u4e49\uff1afunction \u5173\u952e\u5b57\u548c\u533f\u540d\u51fd\u6570\u8868\u8fbe\u5f0f\uff0c\u54ea\u4e2a\u66f4\u9002\u5408\u4f60\uff1f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173102746017554.jpg\" class=\"aligncenter\" title=\"JavaScript \u4e2d\u7684\u51fd\u6570\u5b9a\u4e49\uff1afunction \u5173\u952e\u5b57\u548c\u533f\u540d\u51fd\u6570\u8868\u8fbe\u5f0f\uff0c\u54ea\u4e2a\u66f4\u9002\u5408\u4f60\uff1f\u63d2\u56fe\" alt=\"JavaScript \u4e2d\u7684\u51fd\u6570\u5b9a\u4e49\uff1afunction \u5173\u952e\u5b57\u548c\u533f\u540d\u51fd\u6570\u8868\u8fbe\u5f0f\uff0c\u54ea\u4e2a\u66f4\u9002\u5408\u4f60\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>function\u7684\u4e24\u79cd\u5e38\u89c1\u5199\u6cd5<\/strong><\/p>\n<p>\u5728javascript\u4e2d\uff0c\u6211\u4eec\u901a\u5e38\u4f1a\u4f7f\u7528\u4e24\u79cd\u4e0d\u540c\u7684\u65b9\u5f0f\u6765\u5b9a\u4e49\u51fd\u6570\uff1afunction\u5173\u952e\u5b57\u548c\u533f\u540d\u51fd\u6570\u8868\u8fbe\u5f0f\u3002\u4e0b\u9762\u6211\u4eec\u5c06\u63a2\u8ba8\u8fd9\u4e24\u79cd\u5199\u6cd5\u7684\u548c\u9002\u7528\u573a\u666f\u3002<\/p>\n<p><strong>1. function\u5173\u952e\u5b57<\/strong><\/p>\n<p>\u4f7f\u7528function\u5173\u952e\u5b57\u5b9a\u4e49\u7684\u51fd\u6570\u662f\u4f20\u7edf\u7684javascript\u51fd\u6570\u58f0\u660e\u65b9\u5f0f\u3002\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<pre>function functionname(parameters) {\n  \/\/ \u51fd\u6570\u4f53\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>functionname\u662f\u51fd\u6570\u7684\u540d\u79f0\uff0cparameters\u662f\u51fd\u6570\u7684\u53c2\u6570\u5217\u8868\u3002\u51fd\u6570\u4f53\u5305\u542b\u4e86\u51fd\u6570\u6267\u884c\u7684\u4ee3\u7801\u3002<\/p>\n<p><strong>2. \u533f\u540d\u51fd\u6570\u8868\u8fbe\u5f0f<\/strong><\/p>\n<p>\u533f\u540d\u51fd\u6570\u8868\u8fbe\u5f0f\u662f\u4e00\u79cd\u4e0d\u6307\u5b9a\u51fd\u6570\u540d\u79f0\u7684\u51fd\u6570\u5b9a\u4e49\u65b9\u5f0f\u3002\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre>var variablename = function(parameters) {\n  \/\/ \u51fd\u6570\u4f53\n};<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>variablename\u662f\u5b58\u50a8\u51fd\u6570\u7684\u53d8\u91cf\u540d\uff0cparameters\u662f\u51fd\u6570\u7684\u53c2\u6570\u5217\u8868\u3002\u51fd\u6570\u4f53\u4e5f\u5305\u542b\u4e86\u51fd\u6570\u6267\u884c\u7684\u4ee3\u7801\u3002<\/p>\n<p><strong>\u9002\u7528\u573a\u666f<\/strong><\/p>\n<p>\u4e24\u79cd\u51fd\u6570\u5b9a\u4e49\u65b9\u5f0f\u5404\u6709\u5176\u9002\u7528\u573a\u666f\uff1a<\/p>\n<ul>\n<li> <strong>function\u5173\u952e\u5b57\uff1a<\/strong>\u901a\u5e38\u7528\u4e8e\u5b9a\u4e49\u9700\u8981\u5728\u6a21\u5757\u6216\u5168\u5c40\u8303\u56f4\u5185\u8bbf\u95ee\u7684\u547d\u540d\u51fd\u6570\u3002<\/li>\n<li> <strong>\u533f\u540d\u51fd\u6570\u8868\u8fbe\u5f0f\uff1a<\/strong>\u4e3b\u8981\u7528\u4e8e\u9700\u8981\u5c06\u51fd\u6570\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9\u5176\u4ed6\u51fd\u6570\u6216\u7acb\u5373\u6267\u884c\u7684\u60c5\u51b5\u3002\u53e6\u5916\uff0c\u533f\u540d\u51fd\u6570\u8868\u8fbe\u5f0f\u8fd8\u53ef\u4ee5\u7528\u4e8e\u521b\u5efa\u95ed\u5305\u3002<\/li>\n<\/ul>\n<p><strong>\u5b9e\u4f8b<\/strong><\/p>\n<p>\u6839\u636e\u7ed9\u51fa\u7684\u95ee\u9898\u7b54\u6848\uff0c\u6211\u4eec\u53ef\u4ee5\u4e3e\u51e0\u4e2a\u4f8b\u5b50\u6765\u8bf4\u660e\u8fd9\u4e24\u79cd\u51fd\u6570\u5b9a\u4e49\u65b9\u5f0f\u7684\u4e0d\u540c\uff1a<\/p>\n<p><strong>\u793a\u4f8b 1\uff1a<\/strong><\/p>\n<pre>function test() {\n  return \"\u56ed\";\n}\nalert(test()); \/\/ \u8f93\u51fa\uff1a\"\u56ed\"<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u793a\u4f8b 2\uff1a<\/strong><\/p>\n<pre>var test = function() {\n  return \"\u56ed\";\n}();\nalert(test); \/\/ \u8f93\u51fa\uff1a\"\u56ed\"<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u793a\u4f8b 3\uff1a<\/strong><\/p>\n<pre>var test = {\n  test: function() {\n    return \"\u56ed\";\n  }\n};\nalert(test.test()); \/\/ \u8f93\u51fa\uff1a\"\u56ed\"<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u793a\u4f8b 4\uff1a<\/strong><\/p>\n<pre>(function() {\n  \/\/ \u7acb\u5373\u6267\u884c\u7684\u533f\u540d\u51fd\u6570\u8868\u8fbe\u5f0f\uff0c\u6ca1\u6709\u8fd4\u56de\u503c\uff0c\u53ef\u4ee5\u7528\u4e8e\u6267\u884c\u4e00\u6b21\u6027\u4efb\u52a1\u3002\n  alert(\"Hello, world!\");\n})();<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fJavaScript \u4e2d\u7684\u51fd\u6570\u5b9a\u4e49\uff1afunction \u5173\u952e\u5b57\u548c\u533f\u540d\u51fd\u6570\u8868\u8fbe\u5f0f\uff0c\u54ea\u4e2a\u66f4\u9002\u5408\u4f60\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>function\u7684\u4e24\u79cd\u5e38\u89c1\u5199\u6cd5 \u5728javascript\u4e2d\uff0c\u6211\u4eec\u901a\u5e38\u4f1a\u4f7f\u7528\u4e24\u79cd\u4e0d\u540c\u7684\u65b9\u5f0f\u6765\u5b9a\u4e49\u51fd\u6570\uff1afunction\u5173\u952e\u5b57\u548c\u533f\u540d\u51fd\u6570\u8868\u8fbe\u5f0f\u3002\u4e0b\u9762\u6211\u4eec\u5c06\u63a2\u8ba8\u8fd9\u4e24\u79cd\u5199\u6cd5\u7684\u548c\u9002\u7528\u573a\u666f\u3002 1. function\u5173\u952e\u5b57 \u4f7f\u7528function\u5173\u952e\u5b57\u5b9a\u4e49\u7684\u51fd\u6570\u662f\u4f20\u7edf\u7684javascript\u51fd\u6570\u58f0\u660e\u65b9\u5f0f\u3002\u8bed\u6cd5\u5982\u4e0b\uff1a \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b function functionname(parameters) { \/\/ \u51fd\u6570\u4f53 } \u767b\u5f55\u540e\u590d\u5236 functionname\u662f\u51fd\u6570\u7684\u540d\u79f0\uff0cparameters\u662f\u51fd\u6570\u7684\u53c2\u6570\u5217\u8868\u3002\u51fd\u6570\u4f53\u5305\u542b\u4e86\u51fd\u6570\u6267\u884c\u7684\u4ee3\u7801\u3002 2. \u533f\u540d\u51fd\u6570\u8868\u8fbe\u5f0f \u533f\u540d\u51fd\u6570\u8868\u8fbe\u5f0f\u662f\u4e00\u79cd\u4e0d\u6307\u5b9a\u51fd\u6570\u540d\u79f0\u7684\u51fd\u6570\u5b9a\u4e49\u65b9\u5f0f\u3002\u8bed\u6cd5\u5982\u4e0b\uff1a var variablename = function(parameters) { \/\/ \u51fd\u6570\u4f53 }; \u767b\u5f55\u540e\u590d\u5236 variablename\u662f\u5b58\u50a8\u51fd\u6570\u7684\u53d8\u91cf\u540d\uff0cparameters\u662f\u51fd\u6570\u7684\u53c2\u6570\u5217\u8868\u3002\u51fd\u6570\u4f53\u4e5f\u5305\u542b\u4e86\u51fd\u6570\u6267\u884c\u7684\u4ee3\u7801\u3002 \u9002\u7528\u573a\u666f \u4e24\u79cd\u51fd\u6570\u5b9a\u4e49\u65b9\u5f0f\u5404\u6709\u5176\u9002\u7528\u573a\u666f\uff1a function\u5173\u952e\u5b57\uff1a\u901a\u5e38\u7528\u4e8e\u5b9a\u4e49\u9700\u8981\u5728\u6a21\u5757\u6216\u5168\u5c40\u8303\u56f4\u5185\u8bbf\u95ee\u7684\u547d\u540d\u51fd\u6570\u3002 \u533f\u540d\u51fd\u6570\u8868\u8fbe\u5f0f\uff1a\u4e3b\u8981\u7528\u4e8e\u9700\u8981\u5c06\u51fd\u6570\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9\u5176\u4ed6\u51fd\u6570\u6216\u7acb\u5373\u6267\u884c\u7684\u60c5\u51b5\u3002\u53e6\u5916\uff0c\u533f\u540d\u51fd\u6570\u8868\u8fbe\u5f0f\u8fd8\u53ef\u4ee5\u7528\u4e8e\u521b\u5efa\u95ed\u5305\u3002 \u5b9e\u4f8b \u6839\u636e\u7ed9\u51fa\u7684\u95ee\u9898\u7b54\u6848\uff0c\u6211\u4eec\u53ef\u4ee5\u4e3e\u51e0\u4e2a\u4f8b\u5b50\u6765\u8bf4\u660e\u8fd9\u4e24\u79cd\u51fd\u6570\u5b9a\u4e49\u65b9\u5f0f\u7684\u4e0d\u540c\uff1a \u793a\u4f8b 1\uff1a function test() { return &#8220;\u56ed&#8221;; } alert(test()); \/\/ \u8f93\u51fa\uff1a&#8221;\u56ed&#8221; \u767b\u5f55\u540e\u590d\u5236 \u793a\u4f8b 2\uff1a var test = function() { return &#8220;\u56ed&#8221;; }(); [&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-3280","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/3280","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=3280"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/3280\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=3280"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=3280"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=3280"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}