{"id":2059,"date":"2025-02-26T23:20:13","date_gmt":"2025-02-26T15:20:13","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=2059"},"modified":"2025-02-26T23:20:13","modified_gmt":"2025-02-26T15:20:13","slug":"javascript-%e5%a3%b0%e6%98%8e%e6%8f%90%e5%8d%87-hoisting","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/02\/26\/javascript-%e5%a3%b0%e6%98%8e%e6%8f%90%e5%8d%87-hoisting\/","title":{"rendered":"JavaScript \u58f0\u660e\u63d0\u5347 (Hoisting)"},"content":{"rendered":"\n<p>JavaScript \u4e2d\u7684 <strong>\u58f0\u660e\u63d0\u5347<\/strong> \u662f\u6307\u53d8\u91cf\u548c\u51fd\u6570\u7684\u58f0\u660e\u4f1a\u88ab\u63d0\u5347\u5230\u5f53\u524d\u6267\u884c\u73af\u5883\u7684\u9876\u90e8\u3002\u8fd9\u610f\u5473\u7740\uff0c\u5728\u4ee3\u7801\u7684\u6267\u884c\u8fc7\u7a0b\u4e2d\uff0c\u53d8\u91cf\u548c\u51fd\u6570\u7684\u58f0\u660e\u4f1a\u5728\u4ee3\u7801\u6267\u884c\u524d\u88ab\u5904\u7406\uff0c\u800c\u4e0d\u7ba1\u5b83\u4eec\u5728\u4ee3\u7801\u4e2d\u7684\u5177\u4f53\u4f4d\u7f6e\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>1. \u53d8\u91cf\u58f0\u660e\u63d0\u5347 (var)<\/strong><\/h3>\n\n\n\n<p>\u5728\u4f7f\u7528 <code>var<\/code> \u58f0\u660e\u53d8\u91cf\u65f6\uff0cJavaScript \u4f1a\u5c06\u53d8\u91cf\u7684\u58f0\u660e\uff08\u800c\u4e0d\u662f\u8d4b\u503c\uff09\u63d0\u5347\u5230\u5f53\u524d\u4f5c\u7528\u57df\u7684\u9876\u90e8\u3002\u5373\u4f7f\u53d8\u91cf\u8d4b\u503c\u8bed\u53e5\u5728\u540e\u9762\uff0c\u53d8\u91cf\u4ecd\u7136\u4f1a\u5728\u4ee3\u7801\u7684\u6700\u5f00\u59cb\u5904\u88ab\u201c\u58f0\u660e\u201d\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>\u793a\u4f8b\uff1a\u4f7f\u7528 <code>var<\/code> \u8fdb\u884c\u53d8\u91cf\u58f0\u660e<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>console.log(x);  \/\/ \u8f93\u51fa undefined\uff0c\u56e0\u4e3a x \u5df2\u88ab\u63d0\u5347\uff0c\u4f46\u5c1a\u672a\u8d4b\u503c\nvar x = 5;\nconsole.log(x);  \/\/ \u8f93\u51fa 5<\/code><\/pre>\n\n\n\n<p><strong>\u89e3\u91ca\uff1a<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u5728\u4ee3\u7801\u6267\u884c\u524d\uff0c<code>var x;<\/code> \u4f1a\u88ab\u63d0\u5347\u5230\u9876\u90e8\uff0c\u521d\u59cb\u5316\u4e3a <code>undefined<\/code>\u3002<\/li>\n\n\n\n<li>\u7136\u540e\uff0c<code>x = 5;<\/code> \u4f1a\u5728\u539f\u4f4d\u7f6e\u6267\u884c\uff0c\u56e0\u6b64\u7b2c\u4e00\u6b21 <code>console.log(x)<\/code> \u8f93\u51fa\u7684\u662f <code>undefined<\/code>\uff0c\u7b2c\u4e8c\u6b21\u8f93\u51fa\u7684\u662f <code>5<\/code>\u3002<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2. \u51fd\u6570\u58f0\u660e\u63d0\u5347<\/strong><\/h3>\n\n\n\n<p>\u51fd\u6570\u58f0\u660e\u4f1a\u88ab\u63d0\u5347\u5230\u9876\u90e8\uff0c\u610f\u5473\u7740\u4f60\u53ef\u4ee5\u5728\u51fd\u6570\u5b9a\u4e49\u4e4b\u524d\u8c03\u7528\u8be5\u51fd\u6570\u3002\u8fd9\u9002\u7528\u4e8e\u51fd\u6570\u58f0\u660e\u7684\u60c5\u51b5\uff0c\u4f46\u4e0d\u9002\u7528\u4e8e\u51fd\u6570\u8868\u8fbe\u5f0f\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>\u793a\u4f8b\uff1a\u51fd\u6570\u58f0\u660e\u63d0\u5347<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>console.log(foo());  \/\/ \u8f93\u51fa \"Hello!\"\n\nfunction foo() {\n  return \"Hello!\";\n}<\/code><\/pre>\n\n\n\n<p><strong>\u89e3\u91ca\uff1a<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u5728\u6267\u884c\u4ee3\u7801\u4e4b\u524d\uff0c\u6574\u4e2a\u51fd\u6570\u58f0\u660e\uff08<code>function foo() { ... }<\/code>\uff09\u4f1a\u88ab\u63d0\u5347\u5230\u9876\u90e8\u3002<\/li>\n\n\n\n<li>\u56e0\u6b64\uff0c\u5373\u4f7f <code>foo()<\/code> \u5728\u8c03\u7528\u65f6\u4f4d\u4e8e\u51fd\u6570\u58f0\u660e\u4e4b\u524d\uff0c\u4ee3\u7801\u4ecd\u7136\u80fd\u591f\u6b63\u5e38\u5de5\u4f5c\u3002<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>3. \u51fd\u6570\u8868\u8fbe\u5f0f\u4e0d\u4f1a\u63d0\u5347<\/strong><\/h3>\n\n\n\n<p>\u5982\u679c\u5c06\u51fd\u6570\u8d4b\u503c\u7ed9\u4e00\u4e2a\u53d8\u91cf\uff0c\u90a3\u4e48\u8be5\u53d8\u91cf\u7684\u58f0\u660e\u4f1a\u88ab\u63d0\u5347\uff0c\u4f46\u662f\u51fd\u6570\u7684\u5b9a\u4e49\u4e0d\u4f1a\u88ab\u63d0\u5347\u3002\u56e0\u6b64\uff0c\u5728\u8c03\u7528\u4e4b\u524d\u5f15\u7528\u8be5\u51fd\u6570\u4f1a\u5bfc\u81f4 <code>TypeError<\/code>\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>\u793a\u4f8b\uff1a\u51fd\u6570\u8868\u8fbe\u5f0f\u4e0d\u4f1a\u63d0\u5347<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>console.log(foo());  \/\/ TypeError: foo is not a function\n\nvar foo = function() {\n  return \"Hello!\";\n};<\/code><\/pre>\n\n\n\n<p><strong>\u89e3\u91ca\uff1a<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><code>var foo;<\/code> \u4f1a\u88ab\u63d0\u5347\uff0c\u4f46\u8d4b\u503c <code>foo = function() {...}<\/code> \u4f1a\u4fdd\u6301\u539f\u6765\u7684\u4f4d\u7f6e\u3002<\/li>\n\n\n\n<li>\u7531\u4e8e\u51fd\u6570\u8868\u8fbe\u5f0f\u6ca1\u6709\u88ab\u63d0\u5347\uff0c\u7b2c\u4e00\u6b21\u8c03\u7528 <code>foo()<\/code> \u4f1a\u5bfc\u81f4 <code>TypeError<\/code>\uff0c\u56e0\u4e3a\u5728\u51fd\u6570\u8c03\u7528\u65f6\uff0c<code>foo<\/code> \u7684\u503c\u4e3a <code>undefined<\/code>\u3002<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>4. let \u548c const \u7684\u58f0\u660e\u63d0\u5347<\/strong><\/h3>\n\n\n\n<p><code>let<\/code> \u548c <code>const<\/code> \u4e5f\u6709\u7c7b\u4f3c\u7684\u63d0\u5347\u884c\u4e3a\uff0c\u4f46\u5b83\u4eec\u4e0d\u4f1a\u88ab\u521d\u59cb\u5316\u3002\u5b83\u4eec\u4f1a\u8fdb\u5165\u6240\u8c13\u7684 <strong>&#8220;\u6682\u65f6\u6027\u6b7b\u533a&#8221; (TDZ)<\/strong>\uff0c\u5373\u5728\u58f0\u660e\u4e4b\u524d\u8bbf\u95ee\u5b83\u4eec\u4f1a\u5bfc\u81f4\u9519\u8bef\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>\u793a\u4f8b\uff1alet \u548c const \u7684\u63d0\u5347<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>console.log(a);  \/\/ ReferenceError: Cannot access 'a' before initialization\nlet a = 10;<\/code><\/pre>\n\n\n\n<p><strong>\u89e3\u91ca\uff1a<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><code>let<\/code> \u548c <code>const<\/code> \u4f1a\u88ab\u63d0\u5347\u5230\u9876\u90e8\uff0c\u4f46\u662f\u5b83\u4eec\u4e0d\u4f1a\u521d\u59cb\u5316\u3002\u5728\u58f0\u660e\u4e4b\u524d\u8bbf\u95ee\u5b83\u4eec\u4f1a\u629b\u51fa <code>ReferenceError<\/code>\u3002<\/li>\n\n\n\n<li>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c<code>a<\/code> \u88ab\u63d0\u5347\uff0c\u4f46\u5728\u8bbf\u95ee <code>a<\/code> \u4e4b\u524d\uff0c\u5b83\u8fd8\u6ca1\u6709\u521d\u59cb\u5316\uff0c\u5bfc\u81f4 <code>ReferenceError<\/code>\u3002<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>5. \u603b\u7ed3\uff1a\u63d0\u5347\u7684\u884c\u4e3a<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>var<\/code> \u58f0\u660e\u7684\u53d8\u91cf<\/strong>\u4f1a\u88ab\u63d0\u5347\u5230\u9876\u90e8\uff0c\u4f46\u521d\u59cb\u5316\u503c\u4e0d\u4f1a\u88ab\u63d0\u5347\u3002\u53d8\u91cf\u7684\u521d\u59cb\u503c\u662f <code>undefined<\/code>\uff0c\u76f4\u5230\u6267\u884c\u5230\u8d4b\u503c\u8bed\u53e5\u3002<\/li>\n\n\n\n<li><strong>\u51fd\u6570\u58f0\u660e<\/strong>\u4f1a\u88ab\u63d0\u5347\u5230\u9876\u90e8\uff0c\u610f\u5473\u7740\u4f60\u53ef\u4ee5\u5728\u51fd\u6570\u5b9a\u4e49\u4e4b\u524d\u8c03\u7528\u5b83\u3002<\/li>\n\n\n\n<li><strong>\u51fd\u6570\u8868\u8fbe\u5f0f<\/strong>\u7684\u63d0\u5347\u4ec5\u9650\u4e8e\u53d8\u91cf\u58f0\u660e\u90e8\u5206\uff0c\u51fd\u6570\u672c\u8eab\u4e0d\u4f1a\u88ab\u63d0\u5347\u3002<\/li>\n\n\n\n<li><strong><code>let<\/code> \u548c <code>const<\/code><\/strong> \u58f0\u660e\u7684\u53d8\u91cf\u4f1a\u88ab\u63d0\u5347\u5230\u9876\u90e8\uff0c\u4f46\u8fdb\u5165\u6682\u65f6\u6027\u6b7b\u533a\uff0c\u5728\u58f0\u660e\u4e4b\u524d\u65e0\u6cd5\u8bbf\u95ee\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>6. \u793a\u4f8b\u603b\u7ed3<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ var \u53d8\u91cf\u63d0\u5347\nconsole.log(a);  \/\/ undefined\nvar a = 2;\n\n\/\/ \u51fd\u6570\u58f0\u660e\u63d0\u5347\nconsole.log(foo());  \/\/ Hello!\nfunction foo() {\n  return \"Hello!\";\n}\n\n\/\/ \u51fd\u6570\u8868\u8fbe\u5f0f\u4e0d\u4f1a\u63d0\u5347\nconsole.log(bar());  \/\/ TypeError: bar is not a function\nvar bar = function() {\n  return \"Hi!\";\n}\n\n\/\/ let \u548c const \u63d0\u5347\u5230\u9876\u90e8\uff0c\u4f46\u6709 TDZ\nconsole.log(x);  \/\/ ReferenceError: Cannot access 'x' before initialization\nlet x = 10;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u603b\u7ed3<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u58f0\u660e\u63d0\u5347<\/strong>\u4f1a\u5c06\u53d8\u91cf\u548c\u51fd\u6570\u7684\u58f0\u660e\u90e8\u5206\u63d0\u5347\u5230\u5f53\u524d\u4f5c\u7528\u57df\u7684\u9876\u90e8\u3002<\/li>\n\n\n\n<li><strong><code>var<\/code><\/strong> \u7684\u53d8\u91cf\u4f1a\u88ab\u63d0\u5347\uff0c\u5e76\u521d\u59cb\u5316\u4e3a <code>undefined<\/code>\u3002<\/li>\n\n\n\n<li><strong>\u51fd\u6570\u58f0\u660e<\/strong>\u4f1a\u88ab\u5b8c\u5168\u63d0\u5347\uff08\u5305\u62ec\u51fd\u6570\u4f53\uff09\u3002<\/li>\n\n\n\n<li><strong><code>let<\/code> \u548c <code>const<\/code><\/strong> \u4e5f\u4f1a\u88ab\u63d0\u5347\uff0c\u4f46\u5728\u58f0\u660e\u524d\u8bbf\u95ee\u5b83\u4eec\u4f1a\u5bfc\u81f4\u9519\u8bef\uff08<code>ReferenceError<\/code>\uff09\u3002<\/li>\n\n\n\n<li><strong>\u51fd\u6570\u8868\u8fbe\u5f0f<\/strong>\u7684\u8d4b\u503c\u4e0d\u4f1a\u88ab\u63d0\u5347\uff0c\u51fd\u6570\u5b9a\u4e49\u4f1a\u5728\u4ee3\u7801\u7684\u5b9e\u9645\u4f4d\u7f6e\u6267\u884c\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u66f4\u591a\u8be6\u7ec6\u4fe1\u606f\u8bf7\u5173\u6ce8\u5176\u4ed6\u76f8\u5173\u6587\u7ae0\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>JavaScript \u4e2d\u7684 \u58f0\u660e\u63d0\u5347 \u662f\u6307\u53d8\u91cf\u548c\u51fd\u6570\u7684\u58f0\u660e\u4f1a\u88ab\u63d0\u5347\u5230\u5f53\u524d\u6267\u884c\u73af\u5883\u7684\u9876\u90e8\u3002\u8fd9\u610f\u5473\u7740\uff0c\u5728\u4ee3\u7801\u7684\u6267\u884c [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[46],"tags":[],"class_list":["post-2059","post","type-post","status-publish","format-standard","hentry","category-javascript"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2059","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=2059"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2059\/revisions"}],"predecessor-version":[{"id":2060,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2059\/revisions\/2060"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=2059"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=2059"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=2059"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}