{"id":1115,"date":"2022-04-29T19:56:05","date_gmt":"2022-04-29T11:56:05","guid":{"rendered":"http:\/\/zhuxinyong.com\/?p=1115"},"modified":"2023-02-16T14:40:41","modified_gmt":"2023-02-16T06:40:41","slug":"13-javascript-zhong-de-ti-sheng","status":"publish","type":"post","link":"https:\/\/zhuxinyong.com\/?p=1115","title":{"rendered":"13 &#8211; JavaScript \u4e2d\u7684\u63d0\u5347"},"content":{"rendered":"<p><img decoding=\"async\" src=\"http:\/\/zhuxinyong.com\/wp-content\/uploads\/2022\/04\/16512333709891.jpg\" alt=\"\" \/><\/p>\n<p>\u539f\u6587\u5730\u5740\uff1a<a href=\"https:\/\/dev.to\/bhagatparwinder\/hoisting-in-javascript-2aj2\" target=\"_blank\" rel=\"noopener\">https:\/\/dev.to\/bhagatparwinder\/hoisting-in-javascript-2aj2<\/a><\/p>\n<h3><a id=\"%E4%BB%80%E4%B9%88%E6%98%AF%E6%8F%90%E7%A4%BA%EF%BC%9F\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>\u4ec0\u4e48\u662f\u63d0\u793a\uff1f<\/h3>\n<p>JavaScript \u4e2d\u7684\u63d0\u5347\u5141\u8bb8\u4f60\u5728\u53d8\u91cf\u548c\u51fd\u6570\u58f0\u660e\u4e4b\u524d\u4f7f\u7528\u5b83\u4eec\u3002<\/p>\n<h3><a id=\"%E5%87%BD%E6%95%B0%E6%8F%90%E5%8D%87\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>\u51fd\u6570\u63d0\u5347<\/h3>\n<pre><code class=\"language-plain_text\">console.log(helloWorld()); \/\/ Hello World\nconsole.log(multiplyNumbers(10, 5)); \/\/ 50\n\nfunction helloWorld() {\n    return &quot;Hello World&quot;;\n}\n\nfunction multiplyNumbers(a, b) {\n    return a * b;\n}\n<\/code><\/pre>\n<p>\u5982\u4f60\u6240\u89c1\uff0c\u6211\u5728\u4e24\u4e2a\u51fd\u6570\u58f0\u660e\u4e4b\u524d\u4f7f\u7528\u4e86\u5b83\u4eec\u5e76\u4e14 JavaScript \u6ca1\u6709\u62a5\u9519\u3002\u6253\u5370\u51fa\u4e86\u9884\u671f\u7684\u7ed3\u679c\u3002\u4f7f\u7528\u51fd\u6570\u4e4b\u524d\u5148\u58f0\u660e\u662f\u4e00\u4e2a\u597d\u4e60\u60ef\u3002<\/p>\n<h3><a id=\"var%E5%A3%B0%E6%98%8E%E5%8F%98%E9%87%8F%E6%8F%90%E5%8D%87\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>var \u58f0\u660e\u53d8\u91cf\u63d0\u5347<\/h3>\n<pre><code class=\"language-plain_text\">console.log(age); \/\/ undefined\nconsole.log(foo); \/\/ Reference Error: foo is not defined\n\nvar age = 10;\n<\/code><\/pre>\n<p>\u6211\u53ef\u4ee5\u5728\u53d8\u91cf\u58f0\u660e\u4e4b\u524d\u4f7f\u7528\u5b83\u3002\u6253\u5370\u51fa\u4e86 <strong>undefined<\/strong>\uff0c\u4f46\u5e76\u6ca1\u6709\u629b\u51fa\u9519\u8bef\u3002\u548c\u6253\u5370 <strong>foo<\/strong> \u76f8\u6bd4\uff0c\u4f60\u4f1a\u770b\u5230\u90a3\u6761\u8bed\u53e5\u629b\u51fa\u4e86\u4e00\u4e2a\u9519\u8bef\u3002<\/p>\n<h3><a id=\"let%E5%92%8C-const%E5%A3%B0%E6%98%8E%E5%8F%98%E9%87%8F%E6%8F%90%E7%A4%BA\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>let \u548c const \u58f0\u660e\u53d8\u91cf\u63d0\u793a<\/h3>\n<p>let \u548c const  \u58f0\u660e\u7684\u53d8\u91cf\u4e5f\u4f1a\u63d0\u5347\uff0c\u4f46\u5e76\u4e0d\u50cf var \u58f0\u660e\u7684\u90a3\u6837\u9ed8\u8ba4\u521d\u59cb\u5316\u4e00\u4e2a\u9ed8\u8ba4\u503c undefined\u3002\u5728\u521d\u59cb\u5316\u4e4b\u524d\u8bfb\u53d6\u901a\u8fc7 let \u6216 const \u58f0\u660e\u7684\u53d8\u91cf\u5219\u4f1a\u629b\u51fa\u9519\u8bef\u3002<\/p>\n<pre><code class=\"language-plain_text\">console.log(num); \/\/ Throws ReferenceError exception as the variable value is uninitialized\nlet num = 6; \/\/ Initialization\n<\/code><\/pre>\n<p>\u6ce8\u610f\uff0c\u4ee3\u7801\u7684\u6267\u884c\u987a\u5e8f\u8d77\u5173\u952e\u4f5c\u7528\u800c\u4e0d\u662f\u4ee3\u7801\u5728\u6587\u4ef6\u4e2d\u7684\u4e66\u5199\u987a\u5e8f\u3002\u5728\u4efb\u4f55\u8bfb\u53d6\u5b83\u7684\u4ee3\u7801\u4e4b\u524d\u521d\u59cb\u5316\u5c31\u4e0d\u4f1a\u6709\u62a5\u9519\u3002<\/p>\n<p>\u8fd9\u91cc\u52a0\u4e00\u4e9b TDZ\uff08Temporal dead zone\uff09\u7684\u5185\u5bb9\uff0cTDZ \u662f\u6307\u4ece\u5757\u7ea7\u4f5c\u7528\u57df\u7684\u5f00\u59cb\u5230\u53d8\u91cf\u58f0\u660e\u7ed3\u675f\uff1a<\/p>\n<pre><code class=\"language-plain_text\">{ \/\/ TDZ starts at beginning of scope\n  console.log(bar); \/\/ undefined\n  console.log(foo); \/\/ ReferenceError\n  var bar = 1;\n  let foo = 2; \/\/ End of TDZ (for foo)\n}\n<\/code><\/pre>\n<p>\u4e4b\u6240\u4ee5\u79f0\u4e3a\u201c\u6682\u65f6\u201d\uff0c\u90a3\u662f\u56e0\u4e3a\u533a\u57df\u662f\u6839\u636e\u4ee3\u7801\u7684\u6267\u884c\u987a\u5e8f\uff08\u65f6\u95f4\uff09\u800c\u4e0d\u662f\u4ee3\u7801\u7684\u4e66\u5199\u987a\u5e8f\uff08\u4f4d\u7f6e\uff09\u3002\u4f8b\u5982\uff1a\u4e0b\u9762\u4f8b\u5b50\u53ef\u4ee5\u6b63\u5e38\u5de5\u4f5c\uff0c\u5373\u4f7f\u51fd\u6570\u5728 let \u58f0\u660e\u7684\u53d8\u91cf\u4e4b\u524d\u4f7f\u7528\u5b83\uff0c\u8fd9\u4e2a\u51fd\u6570\u5df2\u7ecf\u51fa\u4e86 TDZ \u7684\u8303\u56f4\u3002<\/p>\n<pre><code class=\"language-plain_text\">{\n    \/\/ TDZ starts at beginning of scope\n    const func = () =&gt; console.log(letVar); \/\/ OK\n\n    \/\/ Within the TDZ letVar access throws `ReferenceError`\n\n    let letVar = 3; \/\/ End of TDZ (for letVar)\n    func(); \/\/ Called outside TDZ!\n}\n<\/code><\/pre>\n<h3><a id=\"%E6%8F%90%E5%8D%87%E7%9A%84%E8%AD%A6%E5%91%8A\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>\u63d0\u5347\u7684\u8b66\u544a<\/h3>\n<p>\u4e3a\u4ec0\u4e48\u6ca1\u6709\u6253\u5370 age \u7684\u503c\u5462\uff1f<\/p>\n<blockquote>\n<p>JavaScript \u53ea\u63d0\u5347\u58f0\u660e\uff0c\u5e76\u4e0d\u63d0\u5347\u521d\u59cb\u5316\u3002<\/p>\n<\/blockquote>\n<p>\u5c31\u50cf\u4f8b\u5b50\u4e2d\u7684 age \uff0c\u53ea\u63d0\u5347\u4e86\u58f0\u660e\u5e76\u6ca1\u6709\u628a\u503c\u8d4b\u503c\u7ed9\u5b83\u3002\u7531\u4e8e\u53ea\u63d0\u5347\u4e86\u58f0\u660e\u8fd9\u4e2a\u539f\u5219\uff0c\u82e5\u4f8b\u5b50\u4e2d\u662f\u51fd\u6570\u8868\u8fbe\u5f0f\u4f60\u5219\u65e0\u6cd5\u4f7f\u7528\u63d0\u5347\u6765\u4f7f\u7528\u5b83\u3002<\/p>\n<pre><code class=\"language-plain_text\">console.log(helloWorld()); \/\/ helloWorld is not defined\nconsole.log(multiplyNumbers(10, 5));\n\nconst helloWorld = function () {\n    return &quot;Hello World&quot;;\n}\n\nconst multiplyNumbers = function (a, b) {\n    return a * b;\n}\n<\/code><\/pre>\n<p>\u66f4\u591a\u53c2\u8003 <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Glossary\/Hoisting\" target=\"_blank\" rel=\"noopener\">MDN<\/a>\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u539f\u6587\u5730\u5740\uff1ahttps:\/\/dev.to\/bhagatparwinder\/hoisting-in-ja&#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":[107,109,6,106,108],"class_list":["post-1115","post","type-post","status-publish","format-standard","hentry","category-javascript","category-all","category-frontend","category-tech","tag-const","tag-hosting","tag-javascript","tag-let","tag-var"],"_links":{"self":[{"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/posts\/1115","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=1115"}],"version-history":[{"count":2,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/posts\/1115\/revisions"}],"predecessor-version":[{"id":1117,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/posts\/1115\/revisions\/1117"}],"wp:attachment":[{"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1115"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1115"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1115"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}