{"id":15,"date":"2007-05-21T00:00:21","date_gmt":"2007-05-21T04:00:21","guid":{"rendered":"http:\/\/prglab.com\/blog\/?page_id=15"},"modified":"2007-08-25T13:37:44","modified_gmt":"2007-08-25T17:37:44","slug":"js-functions","status":"publish","type":"page","link":"https:\/\/www.prglab.com\/blog\/js-tutorial\/js-functions","title":{"rendered":"JavaScript \u51fd\u6570"},"content":{"rendered":"<p>\u51fd\u6570\u662f\u53ef\u4ee5\u91cd\u590d\u4f7f\u7528\u7684\u4ee3\u7801\u5757\uff0c\u53ef\u4ee5\u7531\u4e00\u4e2a\u4e8b\u4ef6\u6267\u884c\uff0c\u6216\u88ab\u8c03\u7528\u6267\u884c\u3002<\/p>\n<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<br \/>\n<strong>JavaScript \u51fd\u6570<\/strong><\/p>\n<p>\u4e3a\u9632\u6b62\u811a\u672c\u5728\u9875\u9762\u52a0\u8f7d\u7684\u65f6\u5019\u5c31\u88ab\u6267\u884c\uff0c\u4f60\u53ef\u4ee5\u5c06\u811a\u672c\u5199\u6210\u51fd\u6570\u3002<\/p>\n<p>\u4e00\u4e2a\u51fd\u6570\u4e2d\u7684\u4ee3\u7801\u53ea\u6709\u5728\u4e8b\u4ef6\u89e6\u53d1\u6216\u51fd\u6570\u88ab\u8c03\u7528\u7684\u65f6\u5019\u624d\u88ab\u6267\u884c\u3002<\/p>\n<p>\u4f60\u53ef\u4ee5\u5728\u9875\u9762\u5185\u7684\u4efb\u4f55\u5730\u65b9\u8c03\u7528\u51fd\u6570\uff08\u5982\u679c\u8fd9\u4e2a\u51fd\u6570\u88ab\u5b58\u50a8\u5728\u4e00\u4e2a\u5916\u90e8.js\u6587\u4ef6\u4e2d\uff0c\u4f60\u751a\u81f3\u53ef\u4ee5\u4ece\u5176\u4ed6\u9875\u9762\u4e2d\u8c03\u7528\u5b83\uff09<\/p>\n<p>\u51fd\u6570\u4e00\u822c\u5b9a\u4e49\u5728\u9875\u9762\u5f00\u59cb\u4f4d\u7f6e\u7684&lt;head&gt; \u90e8\u5206\u4e2d\u3002<\/p>\n<p><strong>\u4f8b\u5b50<\/strong><\/p>\n<table align=\"center\" border=\"0\" cellpadding=\"3\" cellspacing=\"1\" width=\"96%\">\n<tr>\n<td class=\"quote\">&lt;html&gt;<br \/>\n&lt;head&gt;<br \/>\n&lt;script type=&#8221;text\/javascript&#8221;&gt;<br \/>\n<span style=\"color: red\">function displaymessage()<br \/>\n{<br \/>\nalert(&#8220;Hello World!&#8221;)<br \/>\n}<\/span><br \/>\n&lt;\/script&gt;<br \/>\n&lt;\/head&gt;&lt;body&gt;<br \/>\n&lt;form&gt;<br \/>\n&lt;input type=&#8221;button&#8221; value=&#8221;Click me!&#8221;<br \/>\nonclick=&#8221;displaymessage()&#8221; &gt;<br \/>\n&lt;\/form&gt;<br \/>\n&lt;\/body&gt;<br \/>\n&lt;\/html&gt;<\/td>\n<\/tr>\n<\/table>\n<p>\u5728 \u4e0a\u9762\u4f8b\u5b50\u4e2d\uff0c\u5982\u679c\u4ee3\u7801\u884c alert(&#8220;Hello world!!&#8221;) \u6ca1\u6709\u88ab\u5199\u5728\u51fd\u6570\u91cc\u9762\uff0c\u90a3\u4e48\u9875\u9762\u4e00\u70b9\u88ab\u52a0\u8f7d\u65f6\u8fd9\u884c\u4ee3\u7801\u5c31\u4f1a\u88ab\u7acb\u5373\u6267\u884c\u3002\u73b0\u5728\uff0c\u8fd9\u884c\u811a\u672c\u5199\u5728\u4e86\u51fd\u6570\u91cc\u9762\uff0c\u5c31\u53ea\u6709\u5728\u7528\u6237\u70b9\u51fb\u6309\u94ae\u540e\u624d\u4f1a\u88ab\u6267\u884c\u3002\u6211\u4eec\u5728\u6309\u94ae\u4e0a\u589e\u52a0 \u4e86\u4e00\u4e2a\u70b9\u51fb( onClick) \u4e8b\u4ef6\u3002\u5f53\u6309\u94ae\u88ab\u70b9\u51fb\u65f6\u8be5\u4e8b\u4ef6\u88ab\u89e6\u53d1\uff0c\u51fd\u6570 displaymessage() \u5c31\u4f1a\u88ab\u6267\u884c\u3002<\/p>\n<p>\u5728&#8221;JS\u4e8b\u4ef6&#8221;\u4e00\u7ae0\u4e2d\u4f60\u5c06\u4f1a\u5b66\u5230\u66f4\u591a\u5173\u4e8e JavaScript \u4e8b\u4ef6\u7684\u5185\u5bb9\u3002<\/p>\n<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-<br \/>\n<strong>\u600e\u6837\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570<\/strong><\/p>\n<p>\u5b9a\u4e49\u51fd\u6570\u7684\u8bed\u6cd5\u662f\uff1a<\/p>\n<table align=\"center\" border=\"0\" cellpadding=\"3\" cellspacing=\"1\" width=\"96%\">\n<tr>\n<td class=\"quote\">function functionname(var1,var2,&#8230;,varX)<br \/>\n{<br \/>\nsome code<br \/>\n}<\/td>\n<\/tr>\n<\/table>\n<p>var1, var2\u7b49\u662f\u4f20\u5165\u51fd\u6570\u7684\u53d8\u91cf\u548c\u503c\u3002 \u82b1\u62ec\u53f7 {  } \u5b9a\u4e49\u4e86\u51fd\u6570\u7684\u5f00\u59cb\u548c\u7ed3\u675f\u3002<\/p>\n<p><strong>\u6ce8\u610f\uff1a<\/strong> \u4e00\u4e2a\u4e0d\u5e26\u53c2\u6570\u7684\u51fd\u6570\u4e5f\u5fc5\u987b\u5728\u51fd\u6570\u540d\u5b57\u7684\u540e\u9762\u52a0\u62ec\u5f27():<\/p>\n<table align=\"center\" border=\"0\" cellpadding=\"3\" cellspacing=\"1\" width=\"96%\">\n<tr>\n<td class=\"quote\">function functionname()<br \/>\n{<br \/>\nsome code<br \/>\n}<\/td>\n<\/tr>\n<\/table>\n<p><strong>\u6ce8\u610f:<\/strong> \u4e0d\u8981\u5fd8\u8bb0\u5927\u5199\u5b57\u6bcd\u5728 JavaScript\u4e2d\u7684\u91cd\u8981\u6027\uff01 \u5173\u952e\u5b57 function \u5fc5\u987b\u662f\u5c0f\u5199\u5b57\u6bcd\uff0c\u5426\u5219\u5c06\u4f1a\u4ea7\u751f\u4e00\u4e2a JavaScript \u51fa\u9519\uff01 \u800c\u4e14\u6ce8\u610f\u5728\u8c03\u7528\u51fd\u6570\u65f6\u5fc5\u987b\u4f7f\u7528\u4e0e\u51fd\u6570\u5b9a\u4e49\u65f6\u4e00\u6a21\u4e00\u6837\u7684\u5927\u5c0f\u5199\u3002<\/p>\n<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<br \/>\n<strong>\u8fd4\u56de\u8bed\u53e5(return)<\/strong><\/p>\n<p>\u8fd4\u56de\u8bed\u53e5\u662f\u7528\u6765\u8bf4\u660e\u51fd\u6570\u8fd4\u56de\u7684\u6570\u503c\u7684\u3002<br \/>\n\u56e0\u6b64\u6709\u8fd4\u56de\u503c\u5f97\u51fd\u6570\u5fc5\u987b\u4f7f\u7528\u8fd4\u56de\u8bed\u53e5\u3002<\/p>\n<p><strong>\u4f8b\u5b50<\/strong><\/p>\n<p>\u4ee5\u4e0b\u51fd\u6570\u8fd4\u56de\u4e24\u4e2a\u6570\u5b57\u7684\u4e58\u79ef (a \u548c b):<\/p>\n<table align=\"center\" border=\"0\" cellpadding=\"3\" cellspacing=\"1\" width=\"96%\">\n<tr>\n<td class=\"quote\">function prod(a,b)<br \/>\n{<br \/>\nx=a*b<br \/>\nreturn x<br \/>\n}<\/td>\n<\/tr>\n<\/table>\n<p>\u5f53\u4f60\u8c03\u7528\u4ee5\u4e0a\u51fd\u6570\u7684\u65f6\u5019\uff0c\u4f60\u5fc5\u987b\u4f20\u5165\u4e24\u4e2a\u53c2\u6570:<\/p>\n<table align=\"center\" border=\"0\" cellpadding=\"3\" cellspacing=\"1\" width=\"96%\">\n<tr>\n<td class=\"quote\">product=prod(2,3)<\/td>\n<\/tr>\n<\/table>\n<p>\u8fd9\u91cc\u51fd\u6570 prod() \u7684\u8fd4\u56de\u503c\u4e3a6\uff0c\u5b83\u5c06\u88ab\u5b58\u50a8\u5728\u53eb\u505a product \u7684\u53d8\u91cf\u4e2d\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u51fd\u6570\u662f\u53ef\u4ee5\u91cd\u590d\u4f7f\u7528\u7684\u4ee3\u7801\u5757\uff0c\u53ef\u4ee5\u7531\u4e00\u4e2a\u4e8b\u4ef6\u6267\u884c\uff0c\u6216\u88ab\u8c03\u7528\u6267\u884c\u3002 &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211; JavaScript \u51fd\u6570 \u4e3a\u9632\u6b62\u811a\u672c\u5728\u9875\u9762\u52a0\u8f7d\u7684\u65f6\u5019\u5c31\u88ab\u6267\u884c\uff0c\u4f60\u53ef\u4ee5\u5c06\u811a\u672c\u5199\u6210\u51fd\u6570\u3002 \u4e00\u4e2a\u51fd\u6570\u4e2d\u7684\u4ee3\u7801\u53ea\u6709\u5728\u4e8b\u4ef6\u89e6\u53d1\u6216\u51fd\u6570\u88ab\u8c03\u7528\u7684\u65f6\u5019\u624d\u88ab\u6267\u884c\u3002 \u4f60\u53ef\u4ee5\u5728\u9875\u9762\u5185\u7684\u4efb\u4f55\u5730\u65b9\u8c03\u7528\u51fd\u6570\uff08\u5982\u679c\u8fd9\u4e2a\u51fd\u6570\u88ab\u5b58\u50a8\u5728\u4e00\u4e2a\u5916\u90e8.js\u6587\u4ef6\u4e2d\uff0c\u4f60\u751a\u81f3\u53ef\u4ee5\u4ece\u5176\u4ed6\u9875\u9762\u4e2d\u8c03\u7528\u5b83\uff09 \u51fd\u6570\u4e00\u822c\u5b9a\u4e49\u5728\u9875\u9762\u5f00\u59cb\u4f4d\u7f6e\u7684&lt;head&gt; \u90e8\u5206\u4e2d\u3002 \u4f8b\u5b50 &lt;html&gt; &lt;head&gt; &lt;script type=&#8221;text\/javascript&#8221;&gt; function displaymessage() { alert(&#8220;Hello World!&#8221;) } &lt;\/script&gt; &lt;\/head&gt;&lt;body&gt; &lt;form&gt; &lt;input type=&#8221;button&#8221; value=&#8221;Click me!&#8221; onclick=&#8221;displaymessage()&#8221; &gt; &lt;\/form&gt; &lt;\/body&gt; &lt;\/html&gt; \u5728 \u4e0a\u9762\u4f8b\u5b50\u4e2d\uff0c\u5982\u679c\u4ee3\u7801\u884c alert(&#8220;Hello world!!&#8221;) \u6ca1\u6709\u88ab\u5199\u5728\u51fd\u6570\u91cc\u9762\uff0c\u90a3\u4e48\u9875\u9762\u4e00\u70b9\u88ab\u52a0\u8f7d\u65f6\u8fd9\u884c\u4ee3\u7801\u5c31\u4f1a\u88ab\u7acb\u5373\u6267\u884c\u3002\u73b0\u5728\uff0c\u8fd9\u884c\u811a\u672c\u5199\u5728\u4e86\u51fd\u6570\u91cc\u9762\uff0c\u5c31\u53ea\u6709\u5728\u7528\u6237\u70b9\u51fb\u6309\u94ae\u540e\u624d\u4f1a\u88ab\u6267\u884c\u3002\u6211\u4eec\u5728\u6309\u94ae\u4e0a\u589e\u52a0 \u4e86\u4e00\u4e2a\u70b9\u51fb( onClick) \u4e8b\u4ef6\u3002\u5f53\u6309\u94ae\u88ab\u70b9\u51fb\u65f6\u8be5\u4e8b\u4ef6\u88ab\u89e6\u53d1\uff0c\u51fd\u6570 displaymessage() \u5c31\u4f1a\u88ab\u6267\u884c\u3002 \u5728&#8221;JS\u4e8b\u4ef6&#8221;\u4e00\u7ae0\u4e2d\u4f60\u5c06\u4f1a\u5b66\u5230\u66f4\u591a\u5173\u4e8e JavaScript \u4e8b\u4ef6\u7684\u5185\u5bb9\u3002 &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;- \u600e\u6837\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570 \u5b9a\u4e49\u51fd\u6570\u7684\u8bed\u6cd5\u662f\uff1a function functionname(var1,var2,&#8230;,varX) { some code } var1, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":3,"menu_order":8,"comment_status":"open","ping_status":"open","template":"","meta":[],"_links":{"self":[{"href":"https:\/\/www.prglab.com\/blog\/wp-json\/wp\/v2\/pages\/15"}],"collection":[{"href":"https:\/\/www.prglab.com\/blog\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.prglab.com\/blog\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.prglab.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.prglab.com\/blog\/wp-json\/wp\/v2\/comments?post=15"}],"version-history":[{"count":0,"href":"https:\/\/www.prglab.com\/blog\/wp-json\/wp\/v2\/pages\/15\/revisions"}],"up":[{"embeddable":true,"href":"https:\/\/www.prglab.com\/blog\/wp-json\/wp\/v2\/pages\/3"}],"wp:attachment":[{"href":"https:\/\/www.prglab.com\/blog\/wp-json\/wp\/v2\/media?parent=15"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}