{"id":3579,"date":"2024-11-10T13:10:32","date_gmt":"2024-11-10T05:10:32","guid":{"rendered":"https:\/\/fwq.ai\/blog\/3579\/"},"modified":"2024-11-10T13:10:32","modified_gmt":"2024-11-10T05:10:32","slug":"javascript-%e4%b8%ad%e7%9a%84%e5%87%bd%e6%95%b0%e5%a3%b0%e6%98%8e%ef%bc%9ageteventfunction-%e5%92%8c-var-fn-function-%e6%9c%89%e4%bb%80%e4%b9%88%e5%8c%ba%e5%88%ab%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/3579\/","title":{"rendered":"JavaScript \u4e2d\u7684\u51fd\u6570\u58f0\u660e\uff1agetevent:function(){} \u548c var fn = function(){} \u6709\u4ec0\u4e48\u533a\u522b\uff1f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173106273313319.jpg\" class=\"aligncenter\" title=\"JavaScript \u4e2d\u7684\u51fd\u6570\u58f0\u660e\uff1agetevent:function(){} \u548c var fn = function(){} \u6709\u4ec0\u4e48\u533a\u522b\uff1f\u63d2\u56fe\" alt=\"JavaScript \u4e2d\u7684\u51fd\u6570\u58f0\u660e\uff1agetevent:function(){} \u548c var fn = function(){} \u6709\u4ec0\u4e48\u533a\u522b\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>function \u7684\u4f7f\u7528<\/strong><\/p>\n<p>\u5728\u7f16\u5199 javascript \u4ee3\u7801\u65f6\uff0c\u7ecf\u5e38\u9700\u8981\u4f7f\u7528\u51fd\u6570\u6765\u5b9e\u73b0\u7279\u5b9a\u529f\u80fd\u3002\u5bf9\u4e8e\u51fd\u6570\u7684\u58f0\u660e\u548c\u4f7f\u7528\uff0c\u5b58\u5728\u4e24\u79cd\u5e38\u89c1\u65b9\u5f0f\uff1a<\/p>\n<p><strong>getevent:function(){}<\/strong><\/p>\n<ul>\n<li>\u8fd9\u79cd\u65b9\u5f0f\u901a\u5e38\u7528\u4e8e\u4e8b\u4ef6\u5904\u7406\u7a0b\u5e8f\u4e2d\u3002<\/li>\n<li>\n<p>\u4f8b\u5982\uff1a<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<pre>&lt;button onclick=\"getevent:function(){alert('hello world!')}\"&gt;\u70b9\u51fb\u6211&lt;\/button&gt;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5f53\u6309\u94ae\u88ab\u70b9\u51fb\u65f6\uff0c\u5c06\u89e6\u53d1\u8be5\u4e8b\u4ef6\u5904\u7406\u7a0b\u5e8f\uff0c\u5e76\u6267\u884c\u6307\u5b9a\u7684\u51fd\u6570\u3002<\/p>\n<\/li>\n<\/ul>\n<p><strong>var fn = function(){}<\/strong><\/p>\n<ul>\n<li>\u8fd9\u79cd\u65b9\u5f0f\u66f4\u901a\u7528\uff0c\u53ef\u4ee5\u5b9a\u4e49\u672a\u7279\u5b9a\u5173\u8054\u5230\u4e8b\u4ef6\u7684\u51fd\u6570\u3002<\/li>\n<li>\n<p>\u4f8b\u5982\uff1a<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<pre>\/\/ \u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\nvar sayhello = function() {\nalert('hello world!');\n};\n\n\/\/ \u8c03\u7528\u51fd\u6570\nsayhello();<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/li>\n<\/ul>\n<p>\u9009\u62e9\u4f7f\u7528\u54ea\u79cd\u65b9\u5f0f\u53d6\u51b3\u4e8e\u51fd\u6570\u7684\u7528\u9014\u548c\u4e0a\u4e0b\u6587\u3002<\/p>\n<p><strong>\u5176\u4ed6\u793a\u4f8b\u7528\u6cd5<\/strong><\/p>\n<p>\u9664\u4e86\u4e0a\u9762\u63d0\u5230\u7684\u4e24\u79cd\u65b9\u5f0f\u5916\uff0c\u8fd8\u6709\u5176\u4ed6\u4e00\u4e9b\u65b9\u6cd5\u53ef\u4ee5\u4f7f\u7528 function\uff1a<\/p>\n<p><strong>\u65b9\u6cd5 1\uff1a\u5bf9\u8c61\u7684\u65b9\u6cd5<\/strong><\/p>\n<ul>\n<li>\u4f7f\u7528\u5bf9\u8c61\u7684\u5c5e\u6027\u6765\u5b9a\u4e49\u4e00\u4e2a\u65b9\u6cd5\u3002<\/li>\n<li>\n<p>\u4f8b\u5982\uff1a<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<pre>var person = {\nname: 'john doe',\ngreet: function() {\n  alert('hello, my name is ' + this.name);\n}\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/li>\n<\/ul>\n<p><strong>\u65b9\u6cd5 2\uff1a\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012<\/strong><\/p>\n<ul>\n<li>\u5c06\u51fd\u6570\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9\u53e6\u4e00\u4e2a\u51fd\u6570\u3002<\/li>\n<li>\n<p>\u4f8b\u5982\uff1a<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<pre>function applyFunction(fn) {\nfn();\n}\n\napplyFunction(sayHello);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/li>\n<\/ul>\n<p>\u6839\u636e\u5177\u4f53\u9700\u6c42\uff0c\u53ef\u4ee5\u7075\u6d3b\u4f7f\u7528 function \u6765\u5b9e\u73b0\u4e0d\u540c\u7684\u529f\u80fd\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fJavaScript \u4e2d\u7684\u51fd\u6570\u58f0\u660e\uff1agetevent:function(){} \u548c var fn = function(){} \u6709\u4ec0\u4e48\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\u4f7f\u7528 \u5728\u7f16\u5199 javascript \u4ee3\u7801\u65f6\uff0c\u7ecf\u5e38\u9700\u8981\u4f7f\u7528\u51fd\u6570\u6765\u5b9e\u73b0\u7279\u5b9a\u529f\u80fd\u3002\u5bf9\u4e8e\u51fd\u6570\u7684\u58f0\u660e\u548c\u4f7f\u7528\uff0c\u5b58\u5728\u4e24\u79cd\u5e38\u89c1\u65b9\u5f0f\uff1a getevent:function(){} \u8fd9\u79cd\u65b9\u5f0f\u901a\u5e38\u7528\u4e8e\u4e8b\u4ef6\u5904\u7406\u7a0b\u5e8f\u4e2d\u3002 \u4f8b\u5982\uff1a \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b &lt;button onclick=&#8221;getevent:function(){alert(&#8216;hello world!&#8217;)}&#8221;&gt;\u70b9\u51fb\u6211&lt;\/button&gt; \u767b\u5f55\u540e\u590d\u5236 \u5f53\u6309\u94ae\u88ab\u70b9\u51fb\u65f6\uff0c\u5c06\u89e6\u53d1\u8be5\u4e8b\u4ef6\u5904\u7406\u7a0b\u5e8f\uff0c\u5e76\u6267\u884c\u6307\u5b9a\u7684\u51fd\u6570\u3002 var fn = function(){} \u8fd9\u79cd\u65b9\u5f0f\u66f4\u901a\u7528\uff0c\u53ef\u4ee5\u5b9a\u4e49\u672a\u7279\u5b9a\u5173\u8054\u5230\u4e8b\u4ef6\u7684\u51fd\u6570\u3002 \u4f8b\u5982\uff1a \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b \/\/ \u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570 var sayhello = function() { alert(&#8216;hello world!&#8217;); }; \/\/ \u8c03\u7528\u51fd\u6570 sayhello(); \u767b\u5f55\u540e\u590d\u5236 \u9009\u62e9\u4f7f\u7528\u54ea\u79cd\u65b9\u5f0f\u53d6\u51b3\u4e8e\u51fd\u6570\u7684\u7528\u9014\u548c\u4e0a\u4e0b\u6587\u3002 \u5176\u4ed6\u793a\u4f8b\u7528\u6cd5 \u9664\u4e86\u4e0a\u9762\u63d0\u5230\u7684\u4e24\u79cd\u65b9\u5f0f\u5916\uff0c\u8fd8\u6709\u5176\u4ed6\u4e00\u4e9b\u65b9\u6cd5\u53ef\u4ee5\u4f7f\u7528 function\uff1a \u65b9\u6cd5 1\uff1a\u5bf9\u8c61\u7684\u65b9\u6cd5 \u4f7f\u7528\u5bf9\u8c61\u7684\u5c5e\u6027\u6765\u5b9a\u4e49\u4e00\u4e2a\u65b9\u6cd5\u3002 \u4f8b\u5982\uff1a \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b var person = { name: &#8216;john doe&#8217;, greet: function() { alert(&#8216;hello, [&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-3579","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/3579","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=3579"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/3579\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=3579"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=3579"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=3579"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}