{"id":3526,"date":"2024-11-10T17:56:20","date_gmt":"2024-11-10T09:56:20","guid":{"rendered":"https:\/\/fwq.ai\/blog\/3526\/"},"modified":"2024-11-10T17:56:20","modified_gmt":"2024-11-10T09:56:20","slug":"javascript-%e4%b8%ad%e7%9a%84-function-%e6%9c%89%e5%93%aa%e4%ba%9b%e5%b8%b8%e8%a7%81%e7%9a%84%e7%94%a8%e6%b3%95%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/3526\/","title":{"rendered":"JavaScript \u4e2d\u7684 function \u6709\u54ea\u4e9b\u5e38\u89c1\u7684\u7528\u6cd5\uff1f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173105184218288.jpg\" class=\"aligncenter\" title=\"JavaScript \u4e2d\u7684 function \u6709\u54ea\u4e9b\u5e38\u89c1\u7684\u7528\u6cd5\uff1f\u63d2\u56fe\" alt=\"JavaScript \u4e2d\u7684 function \u6709\u54ea\u4e9b\u5e38\u89c1\u7684\u7528\u6cd5\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>function\u7684\u591a\u79cd\u7528\u6cd5<\/strong><\/p>\n<p>\u5728 javascript \u4e2d\uff0cfunction \u626e\u6f14\u7740\u91cd\u8981\u89d2\u8272\uff0c\u5b83\u80fd\u5c01\u88c5\u4ee3\u7801\uff0c\u4f7f\u5176\u53ef\u4ee5\u91cd\u590d\u4f7f\u7528\u3002\u4ee5\u4e0b\u4ecb\u7ecd\u51e0\u79cd\u5e38\u89c1\u7684 function \u4f7f\u7528\u65b9\u5f0f\uff1a<\/p>\n<p><strong>\u5373\u65f6\u6267\u884c\u51fd\u6570<\/strong><\/p>\n<p><strong>\u4f7f\u7528\u573a\u666f\uff1a<\/strong>\u5f53\u9700\u8981\u7acb\u5373\u6267\u884c\u67d0\u6bb5\u4ee3\u7801\uff0c\u800c\u4e0d\u5fc5\u5728\u51fd\u6570\u540d\u524d\u52a0()\u8c03\u7528\u3002<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>var test = function() { return \"\u56ed\"; }();\nalert(test);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5728\u793a\u4f8b\u4e2d\uff0cfunction() \u4f5c\u4e3a var test \u7684\u503c\u88ab\u7acb\u5373\u6267\u884c\uff0c\u5e76\u8d4b\u503c\u7ed9 test \u53d8\u91cf\u3002<\/p>\n<p><strong>\u533f\u540d\u51fd\u6570<\/strong><\/p>\n<p><strong>\u4f7f\u7528\u573a\u666f\uff1a<\/strong>\u5f53\u9700\u8981\u4f7f\u7528\u4e00\u4e2a\u6ca1\u6709\u540d\u79f0\u7684\u51fd\u6570\u3002<\/p>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>getevent: function() { \/* \u4ee3\u7801 *\/ }<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5728\u793a\u4f8b\u4e2d\uff0cfunction() \u4f5c\u4e3a getevent \u4e8b\u4ef6\u7684\u503c\uff0c\u6ca1\u6709\u6307\u5b9a\u540d\u79f0\u3002<\/p>\n<p><strong>\u547d\u540d\u51fd\u6570<\/strong><\/p>\n<p><strong>\u4f7f\u7528\u573a\u666f\uff1a<\/strong>\u5f53\u9700\u8981\u5f15\u7528\u51fd\u6570\uff0c\u6216\u4ece\u5176\u4ed6\u51fd\u6570\u4e2d\u8c03\u7528\u51fd\u6570\u3002<\/p>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>function test() { return \"\u56ed\"; }\nalert(test());<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5728\u793a\u4f8b\u4e2d\uff0ctest() \u662f\u4e00\u4e2a\u547d\u540d\u51fd\u6570\uff0c\u53ef\u4ee5\u88ab\u4ee3\u7801\u4e2d\u7684\u5176\u4ed6\u90e8\u5206\u8c03\u7528\u3002<\/p>\n<p><strong>\u5bf9\u8c61\u65b9\u6cd5<\/strong><\/p>\n<p><strong>\u4f7f\u7528\u573a\u666f\uff1a<\/strong>\u5f53\u9700\u8981\u521b\u5efa\u4e00\u4e2a\u5305\u542b\u65b9\u6cd5\u7684\u5bf9\u8c61\u3002<\/p>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>var Test = {\n  test: function() { return \"\u56ed\"; }\n};\nalert(Test.test());<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5728\u793a\u4f8b\u4e2d\uff0ctest() \u662f test \u5bf9\u8c61\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u7528 test.test() \u6765\u8c03\u7528\u3002<\/p>\n<p>\u8fd9\u4e9b\u53ea\u662f function \u5728 javascript \u4e2d\u5e38\u89c1\u7684\u7528\u6cd5\uff0c\u8fd8\u6709\u66f4\u591a\u7684\u7528\u6cd5\u548c\u53d8\u4f53\uff0c\u53ef\u4ee5\u4f7f\u7528\u9002\u5f53\u7684\u7528\u6cd5\u6765\u6ee1\u8db3\u4e0d\u540c\u7684\u5f00\u53d1\u9700\u6c42\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fJavaScript \u4e2d\u7684 function \u6709\u54ea\u4e9b\u5e38\u89c1\u7684\u7528\u6cd5\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\u591a\u79cd\u7528\u6cd5 \u5728 javascript \u4e2d\uff0cfunction \u626e\u6f14\u7740\u91cd\u8981\u89d2\u8272\uff0c\u5b83\u80fd\u5c01\u88c5\u4ee3\u7801\uff0c\u4f7f\u5176\u53ef\u4ee5\u91cd\u590d\u4f7f\u7528\u3002\u4ee5\u4e0b\u4ecb\u7ecd\u51e0\u79cd\u5e38\u89c1\u7684 function \u4f7f\u7528\u65b9\u5f0f\uff1a \u5373\u65f6\u6267\u884c\u51fd\u6570 \u4f7f\u7528\u573a\u666f\uff1a\u5f53\u9700\u8981\u7acb\u5373\u6267\u884c\u67d0\u6bb5\u4ee3\u7801\uff0c\u800c\u4e0d\u5fc5\u5728\u51fd\u6570\u540d\u524d\u52a0()\u8c03\u7528\u3002 \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b \u793a\u4f8b\uff1a var test = function() { return &#8220;\u56ed&#8221;; }(); alert(test); \u767b\u5f55\u540e\u590d\u5236 \u5728\u793a\u4f8b\u4e2d\uff0cfunction() \u4f5c\u4e3a var test \u7684\u503c\u88ab\u7acb\u5373\u6267\u884c\uff0c\u5e76\u8d4b\u503c\u7ed9 test \u53d8\u91cf\u3002 \u533f\u540d\u51fd\u6570 \u4f7f\u7528\u573a\u666f\uff1a\u5f53\u9700\u8981\u4f7f\u7528\u4e00\u4e2a\u6ca1\u6709\u540d\u79f0\u7684\u51fd\u6570\u3002 \u793a\u4f8b\uff1a getevent: function() { \/* \u4ee3\u7801 *\/ } \u767b\u5f55\u540e\u590d\u5236 \u5728\u793a\u4f8b\u4e2d\uff0cfunction() \u4f5c\u4e3a getevent \u4e8b\u4ef6\u7684\u503c\uff0c\u6ca1\u6709\u6307\u5b9a\u540d\u79f0\u3002 \u547d\u540d\u51fd\u6570 \u4f7f\u7528\u573a\u666f\uff1a\u5f53\u9700\u8981\u5f15\u7528\u51fd\u6570\uff0c\u6216\u4ece\u5176\u4ed6\u51fd\u6570\u4e2d\u8c03\u7528\u51fd\u6570\u3002 \u793a\u4f8b\uff1a function test() { return &#8220;\u56ed&#8221;; } alert(test()); \u767b\u5f55\u540e\u590d\u5236 \u5728\u793a\u4f8b\u4e2d\uff0ctest() [&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-3526","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/3526","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=3526"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/3526\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=3526"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=3526"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=3526"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}