{"id":1108,"date":"2022-04-27T22:50:26","date_gmt":"2022-04-27T14:50:26","guid":{"rendered":"http:\/\/zhuxinyong.com\/?p=1108"},"modified":"2023-02-16T14:40:32","modified_gmt":"2023-02-16T06:40:32","slug":"javascript-zhong-de-han-shu","status":"publish","type":"post","link":"https:\/\/zhuxinyong.com\/?p=1108","title":{"rendered":"10 &#8211; JavaScript \u4e2d\u7684\u51fd\u6570"},"content":{"rendered":"<p><img decoding=\"async\" src=\"http:\/\/zhuxinyong.com\/wp-content\/uploads\/2022\/04\/16510710425431.jpg\" alt=\"\" \/><\/p>\n<p>\u539f\u6587\u5730\u5740\uff1a<a href=\"https:\/\/dev.to\/bhagatparwinder\/functions-in-javascript-5ehm\" target=\"_blank\" rel=\"noopener\">https:\/\/dev.to\/bhagatparwinder\/functions-in-javascript-5ehm<\/a><\/p>\n<h3><a id=\"%E4%BB%80%E4%B9%88%E6%98%AF%E5%87%BD%E6%95%B0%EF%BC%9F%E5%AE%83%E5%9C%A8javascript%E4%B8%AD%E6%89%AE%E6%BC%94%E4%BB%80%E4%B9%88%E8%A7%92%E8%89%B2%EF%BC%9F\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>\u4ec0\u4e48\u662f\u51fd\u6570\uff1f\u5b83\u5728 JavaScript \u4e2d\u626e\u6f14\u4ec0\u4e48\u89d2\u8272\uff1f<\/h3>\n<p>\u51fd\u6570\u5c31\u662f JavaScript \u4e2d\u53ef\u4ee5\u88ab\u6267\u884c\u7684\u4ee3\u7801\u5757\u3002\u51fd\u6570\u6709\u5982\u4e0b\u5fc5\u5907\u90e8\u5206\uff1a<\/p>\n<ol>\n<li>\u4f7f\u7528 function \u5173\u952e\u5b57\u58f0\u660e\uff1b<\/li>\n<li>\u51fd\u6570\u540d\u5b57\u7d27\u8ddf\u5176\u540e\uff0c\u5b83\u5c31\u662f\u88ab\u8c03\u7528\u65f6\u4f7f\u7528\u7684\u540d\u5b57\u3002<\/li>\n<li>\u4f60\u53ef\u4ee5\u7ed9\u51fd\u6570\u4f20\u9012\u53c2\u6570\uff0c\u90a3\u4e9b\u503c\u53ef\u4ee5\u662f\u52a8\u6001\u7684\u3002<\/li>\n<li>\u5f62\u53c2\u4f20\u9012\u7ed9\u51fd\u6570\u7684\u5b9e\u53c2\u3002<\/li>\n<li>\u5f53\u51fd\u6570\u88ab\u8c03\u7528\u65f6\uff0c\u4ee3\u7801\u5757\u5c06\u4f1a\u88ab\u6267\u884c\u3002<\/li>\n<li>\u4ee3\u7801\u5757\u662f\u88ab\u4e2d\u62ec\u53f7\u5305\u88f9\u7684\u3002<\/li>\n<\/ol>\n<p><img decoding=\"async\" src=\"http:\/\/zhuxinyong.com\/wp-content\/uploads\/2022\/04\/16510721248878.jpg\" alt=\"\" \/><\/p>\n<p>\u51fd\u6570\u521b\u5efa\u4e86\u53ef\u590d\u7528\u7684\u4ee3\u7801\u5757\uff0c\u82e5\u4f60\u6709\u9700\u8981\u591a\u6b21\u6267\u884c\u7684\u4ee3\u7801\uff0c\u628a\u5b83\u4eec\u53d8\u4e3a\u4e00\u4e2a\u51fd\u6570\u662f\u4e2a\u597d\u4e3b\u610f\u3002<\/p>\n<h3><a id=\"%E5%87%BD%E6%95%B0%E8%BF%94%E5%9B%9E%E5%80%BC\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>\u51fd\u6570\u8fd4\u56de\u503c<\/h3>\n<p>\u51fd\u6570\u4e00\u65e6\u6267\u884c\u5b8c\u4ee3\u7801\u53ef\u4ee5\u7ed9\u4f60\u8fd4\u56de\u4e00\u4e9b\u4e1c\u897f\uff0c\u4f46\u5e76\u975e\u603b\u662f\u5982\u6b64\u3002\u6709\u65f6\u5019\u51fd\u6570\u6267\u884c\u5b8c\u4e5f\u5c31\u7ed3\u675f\u4e86\u3002<\/p>\n<p>\u51fd\u6570\u6709\u8fd4\u56de\u503c\uff1a<\/p>\n<pre><code class=\"language-plain_text\">function sum(a, b) {\n    return a + b; \/\/ You return the value using return keyword\n}\n\nconst x = sum(2,5);\nconst y = sum(7,9);\nconsole.log(x); \/\/ 7\nconsole.log(y); \/\/ 16\n<\/code><\/pre>\n<p>\u51fd\u6570\u6ca1\u6709\u8fd4\u56de\u503c\uff1a<\/p>\n<pre><code class=\"language-plain_text\">console.log(&quot;Hello World!&quot;);\n\/\/ Hello World\n\/\/ undefined\n<\/code><\/pre>\n<p>\u5982\u679c\u4f60\u5c06\u4e0a\u9762\u4ee3\u7801\u5728\u6d4f\u89c8\u5668\u63a7\u5236\u53f0\u4e2d\u6267\u884c\uff0c\u4f60\u4e0d\u4ec5\u4f1a\u770b\u5230\u63a7\u5236\u53f0\u6253\u5370 \u201cHello World\u201d\uff0c\u8fd8\u4f1a\u5728\u540e\u9762\u6253\u5370 undefined\u3002<\/p>\n<h3><a id=\"javascript%E8%87%AA%E5%B8%A6%E7%9A%84%E5%87%BD%E6%95%B0\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>JavaScript \u81ea\u5e26\u7684\u51fd\u6570<\/h3>\n<p>\u4f60\u4e0d\u662f\u8981\u7ecf\u5e38\u5199\u51fd\u6570\uff0cJavaScript \u81ea\u5e26\u4e86\u8bb8\u591a\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u7684\u65b9\u6cd5\u3002<\/p>\n<pre><code class=\"language-plain_text\">console.log(parseInt()); \/\/ NaN (Not a number)\nconsole.log(parseInt(&quot;234xyz&quot;)); \/\/ 234, parseInt ignore non integer\nconsole.log(Math.random()); \/\/ 0.00746544513267\nconsole.log(Date.now()); \/\/ 1590557812411\n<\/code><\/pre>\n<h2><a id=\"javascript%E4%B8%AD%E5%87%BD%E6%95%B0%E7%9A%84%E7%B1%BB%E5%9E%8B\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>JavaScript \u4e2d\u51fd\u6570\u7684\u7c7b\u578b<\/h2>\n<h3><a id=\"%E5%87%BD%E6%95%B0%E5%A3%B0%E6%98%8E\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>\u51fd\u6570\u58f0\u660e<\/h3>\n<p>\u5f53\u4f60\u4f7f\u7528 function \u5173\u952e\u5b57\u58f0\u660e\u4e86\u4e00\u4e2a\u51fd\u6570\u5e76\u4e14\u6ca1\u6709\u8d4b\u503c\u7ed9\u5176\u4ed6\u53d8\u91cf\uff0c\u8fd9\u5c31\u662f\u51fd\u6570\u58f0\u660e\uff1a<\/p>\n<pre><code class=\"language-plain_text\">function greet(firstName = &quot;new&quot;, lastName = &quot;user&quot;) {\n    return `Hello ${firstName} ${lastName}`;\n}\n\nconsole.log(greet(&quot;Parwinder&quot;, &quot;Bhagat&quot;)); \/\/ Hello Parwinder Bhagat\nconsole.log(greet()); \/\/ Hello new user\n<\/code><\/pre>\n<p>greet \u51fd\u6570\u5c31\u662f\u51fd\u6570\u58f0\u660e\u7684\u4f8b\u5b50\u3002<\/p>\n<p>\u4f60\u53ef\u4ee5\u770b\u5230\u6211\u7b2c\u4e00\u6b21\u51fd\u6570\u8c03\u7528\u65f6\uff0c\u9700\u8981\u7684\u53c2\u6570\u6211\u90fd\u4f20\u4e86\u3002\u4f46\u7b2c\u4e8c\u6b21\u6ca1\u6709\u4f20\u9012\u4efb\u4f55\u53c2\u6570\uff0c\u5b83\u4e00\u6837\u53ef\u4ee5\u6267\u884c\uff0c\u8fd9\u5c31\u662f\u9ed8\u8ba4\u503c\u8d77\u7684\u4f5c\u7528\u3002\u5f53\u8c03\u7528\u8005\u4e0d\u4f1a\u7ed9\u51fd\u6570\u4f20\u53c2\u65f6\uff0c\u4f60\u5c31\u9700\u8981\u8bbe\u7f6e\u9ed8\u8ba4\u503c\u3002<\/p>\n<h3><a id=\"%E5%87%BD%E6%95%B0%E8%A1%A8%E8%BE%BE%E5%BC%8F\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>\u51fd\u6570\u8868\u8fbe\u5f0f<\/h3>\n<p>\u5f53\u4e00\u4e2a\u51fd\u6570\u8d4b\u503c\u7ed9\u4e00\u4e2a\u53d8\u91cf\u79f0\u4e3a\u51fd\u6570\u8868\u8fbe\u5f0f\u3002\u5f53\u8c03\u7528\u4e00\u4e2a\u51fd\u6570\u8868\u8fbe\u5f0f\u65f6\uff0c\u6211\u4eec\u901a\u5e38\u662f\u5728\u4f7f\u7528\u4e00\u4e2a\u533f\u540d\u51fd\u6570\uff08\u6ca1\u6709\u540d\u5b57\u7684\u51fd\u6570\uff09\u3002<\/p>\n<pre><code class=\"language-plain_text\">const greet = function (firstName = &quot;new&quot;, lastName = &quot;user&quot;) {\n    return `Hello ${firstName} ${lastName}`;\n}\n\nconsole.log(greet(&quot;Parwinder&quot;, &quot;Bhagat&quot;)); \/\/ Hello Parwinder Bhagat\nconsole.log(greet()); \/\/ Hello new user\n<\/code><\/pre>\n<p>\u51fd\u6570\u58f0\u660e\u548c\u51fd\u6570\u8868\u8fbe\u5f0f\u5728\u591a\u6570\u573a\u666f\u4e0b\u662f\u4e00\u6837\u7684\u3002\u51fd\u6570\u58f0\u660e\u5728\u5176\u4ed6\u4ee3\u7801\u6267\u884c\u4e4b\u524d\u88ab\u52a0\u8f7d\uff0c\u7136\u800c\u51fd\u6570\u8868\u8fbe\u5f0f\u53ea\u6709\u7b49\u5230 JavaScript \u89e3\u91ca\u5668\u6267\u884c\u5230\u6240\u5728\u4ee3\u7801\u884c\u65f6\u624d\u88ab\u52a0\u8f7d\u3002\u8fd9\u5c31\u662f JavaScript \u4e2d <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Glossary\/Hoisting\" target=\"_blank\" rel=\"noopener\">hoisting<\/a> \u7684\u539f\u7406\u3002\u8fd9\u4e2a\u5185\u5bb9\u6709\u70b9\u8d85\u524d\uff0c\u540e\u9762\u6211\u4eec\u5c06\u4f1a\u8ba8\u8bba\u3002<\/p>\n<h3><a id=\"iife%E7%AB%8B%E5%8D%B3%E6%89%A7%E8%A1%8C%E5%87%BD%E6%95%B0\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>IIFE (\u7acb\u5373\u6267\u884c\u51fd\u6570)<\/h3>\n<p>IIFE \u662f\u4e00\u4e2a\u58f0\u660e\u548c\u6267\u884c\u540c\u65f6\u53d1\u751f\u7684\u51fd\u6570\u3002\u901a\u8fc7\u533f\u540d\u51fd\u6570\u548c\u5c0f\u62ec\u53f7\u6765\u521b\u5efa\uff0c\u7136\u540e\u901a\u8fc7\u5728\u5176\u540e\u589e\u52a0\u4e00\u4e2a\u5c0f\u62ec\u53f7\u8c03\u7528\u3002<\/p>\n<pre><code class=\"language-plain_text\">(function(name){ \/\/ function expression enclosed in ()\n    console.log(`Hello ${name}`); \/\/ Hello Parwinder\n})(&quot;Parwinder&quot;); \/\/ Immediately called by using () in the end. Yes we can pass arguments\n<\/code><\/pre>\n<h3><a id=\"%E7%AE%AD%E5%A4%B4%E5%87%BD%E6%95%B0\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>\u7bad\u5934\u51fd\u6570<\/h3>\n<p>\u7bad\u5934\u51fd\u6570\u662f\u4e00\u4e2a\u7d27\u51d1\u7248\u7684\u51fd\u6570\u8868\u8fbe\u5f0f\uff0c\u4e4b\u6240\u4ee5\u79f0\u4e3a\u7bad\u5934\u51fd\u6570\u662f\u56e0\u4e3a\u4f7f\u7528\u4e86 <strong>=&gt;<\/strong> \u6807\u8bc6\u3002<\/p>\n<pre><code class=\"language-plain_text\">const hello = () =&gt; {\n  return &quot;Hello World!&quot;;\n}\n\nconsole.log(hello()); \/\/ Hello World\n<\/code><\/pre>\n<p>\u5982\u4f60\u6240\u89c1\uff0c\u6211\u4eec\u79fb\u51fa\u4e86 function \u5173\u952e\u5b57\u4f46\u6dfb\u52a0\u4e86\u4e00\u4e2a <strong>=&gt;<\/strong> \u6807\u8bc6\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u5176\u66f4\u77ed\u5c0f\u3002<\/p>\n<pre><code class=\"language-plain_text\">const hello = () =&gt; &quot;Hello World!&quot;;\nconsole.log(hello()); \/\/ Hello World\n<\/code><\/pre>\n<p>\u6211\u4eec\u79fb\u51fa\u4e86 <strong>return<\/strong> \u5173\u952e\u5b57\uff0c\u5f53\u6211\u4eec\u53ea\u6709\u4e00\u6761\u8bed\u53e5\u4e14\u8be5\u8bed\u53e5\u8fd4\u56de\u4e00\u4e2a\u503c\u65f6\uff0c\u8fd9\u6837\u5b8c\u5168\u662f\u53ef\u4ee5\u7684\u3002<\/p>\n<p>\u7bad\u5934\u51fd\u6570\u540c\u6837\u53ef\u4ee5\u63a5\u53d7\u53c2\u6570\u3002<\/p>\n<pre><code class=\"language-plain_text\">const hello = (name) =&gt; `Hello ${name}`;\nconsole.log(hello(&quot;Parwinder&quot;)); \/\/ Hello Parwinder\n<\/code><\/pre>\n<p>\u5f53\u53ea\u6709\u4e00\u4e2a\u53c2\u6570\u65f6\uff0c\u5706\u62ec\u53f7\u53ef\u4ee5\u79fb\u51fa\u3002<\/p>\n<pre><code class=\"language-plain_text\">const hello = name =&gt; `Hello ${name}`;\nconsole.log(hello(&quot;Parwinder&quot;)); \/\/ Hello Parwinder\n<\/code><\/pre>\n<p>\u7bad\u5934\u51fd\u6570\u4e0d\u4ec5\u4ec5\u662f\u66f4\u6f02\u4eae\u548c\u7d27\u51d1\u7684\u901a\u5e38\u7684\u51fd\u6570\u8868\u8fbe\u5f0f\uff0c\u5b83\u4eec\u6ca1\u6709\u4e0e <strong>this<\/strong>\u3001<strong>arguments<\/strong>\u3001<strong>super<\/strong>\u3001<strong>new.target<\/strong>\uff0c\u6211\u4eec\u5c06\u5728\u63a5\u4e0b\u6765\u7684 JavaScript \u6587\u7ae0\u4e2d\u6e29\u4e60\u8fd9\u5199\u6982\u5ff5\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u539f\u6587\u5730\u5740\uff1ahttps:\/\/dev.to\/bhagatparwinder\/functions-in-j&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[249,2,20,3],"tags":[104,6],"class_list":["post-1108","post","type-post","status-publish","format-standard","hentry","category-javascript","category-all","category-frontend","category-tech","tag-function","tag-javascript"],"_links":{"self":[{"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/posts\/1108","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1108"}],"version-history":[{"count":3,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/posts\/1108\/revisions"}],"predecessor-version":[{"id":1118,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/posts\/1108\/revisions\/1118"}],"wp:attachment":[{"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1108"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1108"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1108"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}