{"id":817,"date":"2018-05-05T10:55:13","date_gmt":"2018-05-05T02:55:13","guid":{"rendered":"http:\/\/zouyang1230.com\/blog\/?p=817"},"modified":"2020-10-28T22:46:47","modified_gmt":"2020-10-28T14:46:47","slug":"js%e5%8e%9f%e7%90%86%ef%bc%9a%e5%8f%98%e9%87%8f%e6%8f%90%e5%8d%87%e4%b8%8e%e5%87%bd%e6%95%b0%e6%8f%90%e5%8d%87","status":"publish","type":"post","link":"http:\/\/www.imzouyang.com\/blog\/archives\/817.html","title":{"rendered":"js\u539f\u7406\uff1a\u53d8\u91cf\u63d0\u5347\u4e0e\u51fd\u6570\u63d0\u5347"},"content":{"rendered":"<h4>1\u3001\u9996\u5148\u770b3\u4e2a\u4f8b\u5b50\uff1a<\/h4>\n<pre><code class=\"javascript\">\nconsole.log( a );  \/\/\u4f1a\u6253\u5370\u51fa\u4ec0\u4e48\u5462\uff1f\nvar a = 1;\n\nb = 2;\nvar b;\nconsole.log( b );  \/\/\u4f1a\u6253\u5370\u51fa\u4ec0\u4e48\u5462\uff1f\n\nfoo();  \/\/\u4f1a\u6253\u5370\u51fa\u4ec0\u4e48\u5462\uff1f\nvar foo;\nfunction foo() {\n  console.log( 1 );\n}\nfoo = function() {\n  console.log( 2 );\n};\n<\/code><\/pre>\n<p>\u5982\u679c\u4f60\u5bf9\u4ee5\u4e0a\u4f8b\u5b50\u7684\u6267\u884c\u539f\u7406\u8fd8\u4e0d\u592a\u719f\u6089\uff0c\u4e0d\u8981\u7d27\uff0c\u8ddf\u7740\u672c\u6587\u7684\u4ecb\u7ecd\uff0c\u4f60\u4f1a\u5bf9\u6b64\u6709\u6240\u638c\u63e1\u7684\u3002\uff08\u5bf9\u7ed3\u8bba\u611f\u5174\u8da3\u7684\u53ef\u4ee5\u76f4\u63a5\u8df3\u5230\u672c\u6587\u6700\u540e\u603b\u7ed3\uff09<\/p>\n<p>\u00a0<\/p>\n<h4>2\u3001\u53d8\u91cf\u7684\u63d0\u5347<\/h4>\n<p>\u4f17\u6240\u5468\u77e5 js \u7684\u4ee3\u7801\u6267\u884c\u987a\u5e8f\u4e00\u822c\u662f\u4ece\u4e0a\u800c\u4e0b\u7684\u4e00\u6761\u6761\u6267\u884c\u3002\u4f46\u662f\u5728\u8fdb\u884c\u53d8\u91cf\u7533\u660e\u548c\u51fd\u6570\u7533\u660e\u65f6\uff0c\u4f1a\u6709\u63d0\u5347\u7684\u60c5\u51b5\u3002\u63a5\u4e0b\u6765\u6211\u4eec\u770b\u770b js \u662f\u5982\u4f55\u63d0\u5347\u53d8\u91cf\u548c\u51fd\u6570\u58f0\u660e\u7684\u3002<br \/>js \u4ee3\u7801\u5728\u88ab\u7f16\u8bd1\u5668\u7f16\u8bd1\u65f6\uff0c\u4f1a\u7ecf\u8fc73\u4e2a\u9636\u6bb5\uff1a\u8bcd\u6cd5\u5206\u6790\u3001\u8bed\u6cd5\u5206\u6790\u3001\u4ee3\u7801\u751f\u6210\uff1b\u53d8\u91cf\u7684\u63d0\u5347\u53d1\u751f\u5728\u8bed\u6cd5\u5206\u6790\u9636\u6bb5\u3002<br \/>\u5f53\u6211\u4eec\u5728\u7533\u660e\u4e00\u4e2a\u53d8\u91cf\u65f6\uff0c\u6bd4\u5982 var a = 1;<br \/>\u6211\u4eec\u4e60\u60ef\u5c06 var a = 1; \u770b\u4f5c\u4e00\u4e2a\u58f0\u660e\uff0c \u800c\u5b9e\u9645\u4e0a JavaScript \u5f15\u64ce\u5e76\u4e0d\u8fd9\u4e48\u8ba4\u4e3a\u3002 \u5b83\u5c06 var a \u548c a = 1 \u5f53\u4f5c\u4e24\u4e2a\u5355\u72ec\u7684\u58f0\u660e\uff0c \u7b2c\u4e00\u4e2a\u662f\u7f16\u8bd1\u9636\u6bb5\u7684\u4efb\u52a1\uff0c \u800c\u7b2c\u4e8c\u4e2a\u5219\u662f\u6267\u884c\u9636\u6bb5\u7684\u4efb\u52a1\uff0c\u5177\u4f53\u5982\u4e0b\uff1a<br \/><strong>1\uff09\u9996\u5148<\/strong>\uff0c\u4f1a\u8fdb\u884c\u8bcd\u6cd5\u5206\u6790\uff0c\u627e\u51fa\u54ea\u4e9b\u8bcd\u662f\u4ee3\u7801\u8bed\u53e5\uff0c\u4e0a\u4f8b\u7533\u660e\u4f1a\u88ab\u62c6\u5206\u4e3a\uff1a<strong>var<\/strong>\u3001<strong>a<\/strong>\u3001<strong>=<\/strong>\u3001<strong>1<\/strong>\u3001<strong>;<\/strong> \u8fd95\u4e2a\u8bcd\u6cd5\u5355\u5143\uff08\u53ef\u80fd\u6211\u4eec\u4f1a\u89c9\u5f97\u8fd9\u4e0d\u662f\u5e9f\u8bdd\u5417\uff0c\u5176\u5b9e\u8fd9\u4e2a\u8fc7\u7a0b\u662f\u627e\u51fa\u90a3\u4e9b\u6b63\u786e\u7684\u4ee3\u7801\u8bcd\u6cd5\u5355\u5143\uff0c\u4ee5\u4fbf\u540e\u7eed\u7684\u5206\u6790\uff0c\u6bd5\u7adf\u6709\u65f6\u5f00\u53d1\u8005\u5728\u4e66\u5199 js \u4ee3\u7801\u65f6\u4f1a\u6709\u4e0d\u4e25\u683c\u7684\u60c5\u51b5\uff0c\u6bd4\u5982\u6709\u7684\u5f00\u53d1\u8005\u5fd8\u8bb0\u5206\u53f7\u3001\u591a\u4f59\u6216\u5c11\u7a7a\u683c\u7b49\u7b49\u4e4b\u7c7b\u4e0d\u89c4\u8303\uff09<br \/><strong>2\uff09\u5176\u6b21<\/strong>\uff0c\u4f1a\u8fdb\u884c\u8bed\u6cd5\u5206\u6790\uff0c\u7ec4\u88c5\u8bcd\u6cd5\u5355\u5143\u5e76\u5206\u6790\uff0c\u4f1a\u5c06\u8bcd\u6cd5\u5355\u5143\u6d41\uff08\u6570\u7ec4\uff09\u8f6c\u6362\u6210\u4e00\u4e2a\u7531\u5143\u7d20\u9010\u7ea7\u5d4c\u5957\u800c\u6210\u7684\u201c\u62bd\u8c61\u8bed\u6cd5\u6811\u201d\uff0c\u4e0a\u4f8b var a = 1\uff0cjs \u5f15\u64ce\u4f1a\u5c06\u5176\u5206\u89e3\u62102\u4e2a\u7533\u660e\uff1a<br \/>var a;<br \/>a = 1;<br \/>\u7f16\u8bd1\u5668\u5728\u9047\u5230 var a \u65f6\uff0c\u7f16\u8bd1\u5668\u5728\u8bcd\u6cd5\u5206\u6790\u65f6\u4f1a\u8be2\u95ee\u4f5c\u7528\u57df\u5185\u662f\u5426\u5df2\u7ecf\u5b58\u5728\u4e00\u540d\u540d\u5b57\u53eb\u505a a \u7684\u53d8\u91cf\uff0c\u5982\u679c\u662f\uff0c\u5219\u7f16\u8bd1\u5668\u4f1a<strong>\u5ffd\u7565<\/strong>\u8be5\u58f0\u660e\u7ee7\u7eed\u8fdb\u884c\u7f16\u8bd1\uff0c\u5426\u5219\u5b83\u4f1a\u5728\u5f53\u524d\u4f5c\u7528\u57df\u7684\u96c6\u5408\u5185\u7533\u660e\u4e00\u4e2a\u65b0\u7684\u53d8\u91cf\uff0c\u5e76\u547d\u540d\u4e3a a\uff0c\u53c8\u56e0\u4e3a\u6bcf\u4e2a\u4f5c\u7528\u57df\u96c6\u5408\u662f\u5728\u8be5\u4f5c\u7528\u57df\u4ee3\u7801\u7684\u8fd0\u884c\u65f6\u6700\u5f00\u59cb\u6267\u884c\u7684\uff0c\u6240\u4ee5\u5c31\u4f1a\u51fa\u73b0 \u53d8\u91cf\u58f0\u660e \u88ab\u63d0\u524d\u7684\u60c5\u5f62\u3002<br \/><strong>3\uff09\u6700\u540e<\/strong>\uff0c\u4f1a\u8fdb\u884c\u4ee3\u7801\u751f\u6210\uff0c\u7f16\u8bd1\u5668\u4f1a\u4e3a js \u5f15\u64ce\u751f\u6210\u8fd0\u884c\u65f6\u6240\u9700\u7684\u4ee3\u7801\uff0c\u8fd9\u4e9b\u4ee3\u7801\u88ab\u7528\u6765\u5904\u7406 a = 1 \u8fd9\u4e2a\u8d4b\u503c\u64cd\u4f5c\u3002<br \/>\u00a0<br \/>\u7efc\u4e0a\uff0c\u6240\u4ee5\u7b2c\u4e00\u4e2a\u4f8b\u5b50\uff1a<\/p>\n<pre><code class=\"javascript\">\nconsole.log( a );\nvar a = 1;\n<\/code><\/pre>\n<p>\u5b9e\u9645\u6267\u884c\u6d41\u7a0b\u662f\uff1a<\/p>\n<pre><code class=\"javascript\">\nvar a;\nconsole.log( a );  \/\/\u4f1a\u6253\u5370\u51fa undefined\na = 1;\n<\/code><\/pre>\n<p>\u00a0<br \/>\u7b2c\u4e8c\u4e2a\u4f8b\u5b50\uff1a<\/p>\n<pre><code class=\"javascript\">\nb = 2;\nvar b;\nconsole.log( b );\n<\/code><\/pre>\n<p>\u5b9e\u9645\u6267\u884c\u6d41\u7a0b\u662f\uff1a<\/p>\n<pre><code class=\"javascript\">\nvar b;\nb = 2;\nconsole.log( b );  \/\/\u4f1a\u6253\u5370\u51fa 2\n<\/code><\/pre>\n<p>\u00a0<\/p>\n<h4>3\u3001\u5173\u4e8e\u51fd\u6570\u7684\u63d0\u5347<\/h4>\n<p>\u51fd\u6570\u58f0\u660e\u8ddf\u53d8\u91cf\u58f0\u660e\u4e00\u6837\u4f1a\u88ab\u63d0\u5347\u3002<\/p>\n<p>\u00a0<\/p>\n<h4>\u603b\u7ed3\uff1a<\/h4>\n<p>1\u3001\u65e0\u8bba\u4f5c\u7528\u57df\u4e2d\u7684\u58f0\u660e\u51fa\u73b0\u5728\u4ec0\u4e48\u5730\u65b9\uff0c \u90fd\u5c06\u5728\u4ee3\u7801\u672c\u8eab\u88ab\u6267\u884c\u524d\u9996\u5148\u8fdb\u884c\u5904\u7406\u3002\u53ef\u4ee5\u5c06\u8fd9\u4e2a\u8fc7\u7a0b\u5f62\u8c61\u5730\u60f3\u8c61\u6210\u6240\u6709\u7684\u58f0\u660e\uff08\u53d8\u91cf\u548c\u51fd\u6570\uff09 \u90fd\u4f1a\u88ab\u201c\u79fb\u52a8\u201d \u5230\u5404\u81ea\u4f5c\u7528\u57df\u7684\u6700\u9876\u7aef\uff0c \u8fd9\u4e2a\u8fc7\u7a0b\u88ab\u79f0\u4e3a\u63d0\u5347\u3002<br \/>2\u3001\u53d8\u91cf\u5728\u7533\u660e\u65f6\uff0c\u4f1a\u51fa\u73b0\u63d0\u5347\uff0c\u53ea\u8981\u662f var \u4e00\u4e2a\u53d8\u91cf\uff0c\u8be5\u53d8\u91cf\u7533\u660e\u4f1a\u88ab\u63d0\u5347\u5230\u5f53\u524d\u4f5c\u7528\u57df\u7684\u6700\u5f00\u59cb\u90e8\u5206\u3002<br \/>3\u3001\u51fd\u6570\u5728\u7533\u660e\u65f6\uff0c\u4f1a\u51fa\u73b0\u63d0\u5347\uff0c\u8be5\u51fd\u6570\u7533\u660e\u4f1a\u88ab\u63d0\u5347\u5230\u5f53\u524d\u4f5c\u7528\u57df\u7684\u6700\u5f00\u59cb\u90e8\u5206\u3002<br \/>4\u3001\u53e6\u5916\u503c\u5f97\u6ce8\u610f\u7684\u662f\uff0c\u6bcf\u4e2a\u4f5c\u7528\u57df\u90fd\u4f1a\u8fdb\u884c\u63d0\u5347\u64cd\u4f5c\uff0c\u6bd4\u5982\uff1a<\/p>\n<pre><code class=\"javascript\">\nfoo();\nfunction foo() {\n  console.log( a ); \/\/ undefined\n  var a = 1;\n}\n<\/code><\/pre>\n<p>\u5b9e\u9645\u6267\u884c\u6d41\u7a0b\u662f\uff1a<\/p>\n<pre><code class=\"javascript\">\nfunction foo() {\n  var a;\n  console.log( a ); \/\/ undefined\n  a = 1;\n}\nfoo();\n<\/code><\/pre>\n<p>\u00a0<br \/><br \/><\/p>\n\n\n<p>PS\uff1a\u672c\u6587\u4e0d\u8ba8\u8bba ES6 \u7684 let \u7533\u8bf7\u53d8\u91cf\u7684\u65b9\u5f0f\u5e26\u6765\u7684\u6539\u53d8\uff08\u6bd4\u5982\uff0c\u6ca1\u6709\u53d8\u91cf\u63d0\u5347\u3001\u4e0d\u53ef\u91cd\u590d\u7533\u660e\u53d8\u91cf\u3001\u4f5c\u7528\u57df\u53ea\u5728\u5b83\u7533\u660e\u65f6\u6240\u5728\u7684\u4ee3\u7801\u5757\u5185\u6709\u6548\uff09<br><br>(\u5b8c)<\/p>\n","protected":false},"excerpt":{"rendered":"<p>1\u3001\u9996\u5148\u770b3\u4e2a\u4f8b\u5b50\uff1a console.log( a ); \/\/\u4f1a\u6253\u5370\u51fa\u4ec0\u4e48\u5462\uff1f var a = 1; b =  &hellip; <a href=\"http:\/\/www.imzouyang.com\/blog\/archives\/817.html\" class=\"read-more\">\u7ee7\u7eed\u9605\u8bfb <span class=\"screen-reader-text sr-only\">js\u539f\u7406\uff1a\u53d8\u91cf\u63d0\u5347\u4e0e\u51fd\u6570\u63d0\u5347<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"aside","meta":[],"categories":[30],"tags":[],"_links":{"self":[{"href":"http:\/\/www.imzouyang.com\/blog\/wp-json\/wp\/v2\/posts\/817"}],"collection":[{"href":"http:\/\/www.imzouyang.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.imzouyang.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.imzouyang.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.imzouyang.com\/blog\/wp-json\/wp\/v2\/comments?post=817"}],"version-history":[{"count":4,"href":"http:\/\/www.imzouyang.com\/blog\/wp-json\/wp\/v2\/posts\/817\/revisions"}],"predecessor-version":[{"id":2142,"href":"http:\/\/www.imzouyang.com\/blog\/wp-json\/wp\/v2\/posts\/817\/revisions\/2142"}],"wp:attachment":[{"href":"http:\/\/www.imzouyang.com\/blog\/wp-json\/wp\/v2\/media?parent=817"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.imzouyang.com\/blog\/wp-json\/wp\/v2\/categories?post=817"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.imzouyang.com\/blog\/wp-json\/wp\/v2\/tags?post=817"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}