{"id":555,"date":"2021-05-27T15:07:07","date_gmt":"2021-05-27T07:07:07","guid":{"rendered":"http:\/\/blog.yzmedu.com\/?p=555"},"modified":"2021-05-27T15:08:44","modified_gmt":"2021-05-27T07:08:44","slug":"var%e3%80%81let%e5%92%8cconst%e7%9a%84%e5%8c%ba%e5%88%ab","status":"publish","type":"post","link":"http:\/\/blog.yzmedu.com\/?p=555","title":{"rendered":"var\u3001let\u548cconst\u7684\u533a\u522b"},"content":{"rendered":"\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" width=\"750\" height=\"340\" src=\"http:\/\/blog.yzmedu.com\/wp-content\/uploads\/2021\/05\/srchttp___pic4.zhimg_.com_v2-8b645ed690b93f78bad8d0cea71eb0f7_1200x500.jpgreferhttp___pic4.zhimg_.jpg\" alt=\"\" class=\"wp-image-556\" srcset=\"http:\/\/blog.yzmedu.com\/wp-content\/uploads\/2021\/05\/srchttp___pic4.zhimg_.com_v2-8b645ed690b93f78bad8d0cea71eb0f7_1200x500.jpgreferhttp___pic4.zhimg_.jpg 750w, http:\/\/blog.yzmedu.com\/wp-content\/uploads\/2021\/05\/srchttp___pic4.zhimg_.com_v2-8b645ed690b93f78bad8d0cea71eb0f7_1200x500.jpgreferhttp___pic4.zhimg_-300x136.jpg 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure>\n\n\n\n<p><strong>var \u548c let \u7684\u533a\u522b\u662f\u8001\u751f\u5e38\u8c08\uff0c\u770b\u5230\u7f51\u4e0a\u4e00\u4e9b\u6587\u7ae0\u7684\u603b\u7ed3\uff0c\u6709\u7684\u4e0d\u592a\u5168\u9762\uff0c\u751a\u81f3\u6709\u7684\u63cf\u8ff0\u4e0d\u592a\u51c6\u786e\uff0c\u5728\u8fd9\u91cc\u5c3d\u91cf\u5168\u9762\u7684\u603b\u7ed3\u4e0b\u8fd9\u4e09\u8005\u7684\u533a\u522b\u3002<\/strong><\/p>\n\n\n\n<p><strong>1. let \u4f7f\u7528\u5757\u7ea7\u4f5c\u7528\u57df<\/strong><\/p>\n\n\n\n<p>\u5728 ES6\u4e4b\u524d\uff0cES5\u4e2djs\u53ea\u6709\u5168\u5c40\u4f5c\u7528\u57df\u548c\u51fd\u6570\u4f5c\u7528\u57df\uff0c\u4f8b\u5982\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>if(true) {\n   var a = 'name'\n}\nconsole.log('a',a) \/\/ name<\/code><\/pre>\n\n\n\n<p>\u4f5c\u7528\u57df\u662f\u4e00\u4e2a\u72ec\u7acb\u7684\u5730\u76d8\uff0c\u8ba9\u53d8\u91cf\u4e0d\u5916\u6cc4\u51fa\u53bb\uff0c\u4f46\u662f\u4e0a\u4f8b\u4e2d\u7684\u53d8\u91cf\u5c31\u5916\u6cc4\u4e86\u51fa\u53bb\uff0c\u6240\u4ee5\u6b64\u65f6 JS \u6ca1\u6709\u5757\u7ea7\u4f5c\u7528\u57df\u7684\u6982\u5ff5\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>var a = 1;\nfunction fn() {\n   var a = 2;\n   console.log('fn',a);\n}\nconsole.log('global',a);\nfn();<\/code><\/pre>\n\n\n\n<p>\u5168\u5c40\u4f5c\u7528\u57df\u5c31\u662f\u6700\u5916\u5c42\u7684\u4f5c\u7528\u57df\uff0c\u5982\u679c\u6211\u4eec\u5199\u4e86\u5f88\u591a\u884c JS \u4ee3\u7801\uff0c\u53d8\u91cf\u5b9a\u4e49\u90fd\u6ca1\u6709\u7528\u51fd\u6570\u5305\u62ec\uff0c\u90a3\u4e48\u5b83\u4eec\u5c31\u5168\u90e8\u90fd\u5728\u5168\u5c40\u4f5c\u7528\u57df\u4e2d\u3002\u8fd9\u6837\u7684\u574f\u5904\u5c31\u662f\u5f88\u5bb9\u6613\u51b2\u7a81\u3002<br>ES6\u4e2d\u52a0\u5165\u5757\u7ea7\u4f5c\u7528\u57df\u4e4b\u540e\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>if(true) {\n   let a = 'name'\n}\nconsole.log('a',a) \/\/ Uncaught ReferenceError: a is not defined<\/code><\/pre>\n\n\n\n<p>\u5757\u4f5c\u7528\u57df\u5185\u7528 let \u58f0\u660e\u7684\u53d8\u91cf\uff0c\u5728\u5757\u5916\u662f\u4e0d\u53ef\u89c1\u7684\uff0c\u5982\u679c\u5f15\u7528\u7684\u8bdd\u5c31\u4f1a\u62a5\u9519\u3002<\/p>\n\n\n\n<p><strong>2. let \u7ea6\u675f\u4e86\u53d8\u91cf\u63d0\u5347\u800c\u4e0d\u662f\u6ca1\u6709\u53d8\u91cf\u63d0\u5347<\/strong><\/p>\n\n\n\n<p>\u5728 js \u4e2d\u53d8\u91cf\u548c\u51fd\u6570\u90fd\u4f1a\u63d0\u5347:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>function fn() {\n   console.log('a',a);\n   var a = 1;  \/\/ undefind\n}\nfn()<\/code><\/pre>\n\n\n\n<p>a\u5176\u5b9e\u5df2\u7ecf\u5728\u8c03\u7528\u524d\u88ab\u58f0\u660e\u4e86\uff0c\u53ea\u662f\u6ca1\u6709\u88ab\u521d\u59cb\u5316\u3002JavaScript\u4f1a\u628a\u4f5c\u7528\u57df\u91cc\u7684\u6240\u6709\u53d8\u91cf\u548c\u51fd\u6570\u63d0\u5230\u51fd\u6570\u7684\u9876\u90e8\u58f0\u660e,\u76f8\u5f53\u4e8e\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>function fn() {\n   var a;\n   console.log('a',a);\n   a = 1;  \/\/ undefind\n}\nfn()<\/code><\/pre>\n\n\n\n<p>JavaScript\u4f1a\u4f7f\u7528<code>undefined<\/code>\u7f3a\u7701\u503c\u521b\u5efa\u53d8\u91cfa,\u4e8b\u5b9e\u4e0a\u6d4f\u89c8\u5668\u5e76\u6ca1\u6709\u628a\u58f0\u660e\u8bed\u53e5\u653e\u5230\u4f5c\u7528\u57df\u7684\u9876\u90e8\uff0c\u5728\u7f16\u8bd1\u9636\u6bb5\uff0c\u63a7\u5236\u6d41\u8fdb\u5165\u57df\uff0c\u8be5\u57df\u6240\u6709\u7684\u53d8\u91cf\u548c\u51fd\u6570\u7684\u58f0\u660e\u5148\u8fdb\u5165\u5185\u5b58\uff0c\u6587\u4e2d\u4ee3\u7801\u7684\u76f8\u5bf9\u4f4d\u7f6e\u4e0d\u4f1a\u53d8\u52a8\u3002<\/p>\n\n\n\n<p>\u53d8\u91cf\u63d0\u5347\u6307\u7684\u662f\u53d8\u91cf\u58f0\u660e\u7684\u63d0\u5347\uff0c\u4e0d\u4f1a\u63d0\u5347\u53d8\u91cf\u7684\u521d\u59cb\u5316\u548c\u8d4b\u503c\u3002<\/p>\n\n\n\n<p>\u5e76\u4e14\u51fd\u6570\u7684\u63d0\u5347\u4f18\u5148\u7ea7\u5927\u4e8e\u53d8\u91cf\u7684\u63d0\u5347:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>function fn() {\r\n    console.log('a', a);\r\n    var a = 1;\r\n    function a () {\r\n        console.log('I am a function');\r\n    }\r\n}\r\nfn() \/\/ \u0192 a () {console.log('I am a function');}<\/code><\/pre>\n\n\n\n<p>\u5728\u4e0a\u4f8b\u4e2d\uff0c let \u58f0\u660e\u7684\u53d8\u91cf\u7684\u4f5c\u7528\u57df\u4e4b\u5916\u5f15\u7528\u8be5\u53d8\u91cf\u4f1a\u62a5\u9519\uff0c\u4f46\u662f\u5426\u53ef\u7406\u89e3\u4e3a let \u6ca1\u6709\u53d8\u91cf\u63d0\u5347\uff1f<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>let a = 'outside';\nif(true) {\n   console.log(a);\/\/Uncaught ReferenceError: a is not defined\n    let a = \"inside\";\n}<\/code><\/pre>\n\n\n\n<p>\u62a5\u51fa\u9519\u8bef a \u6ca1\u6709\u88ab\u5b9a\u4e49\uff0c\u800c\u4e0d\u662f\u5f15\u7528\u4e86\u5168\u5c40\u4f5c\u7528\u57df\u91cc\u7684 a\uff0c\u8bf4\u660e let \u58f0\u660e\u7684 a \u4e5f\u88ab\u63d0\u5347\u4e86\u3002<\/p>\n\n\n\n<p>\u539f\u56e0\u662f let \u8bbe\u8ba1\u4e2d\u7684\u6682\u65f6\u6027\u6b7b\u533a\uff1a<br>\u5f53\u524d\u4f5c\u7528\u57df\u9876\u90e8\u5230\u8be5\u53d8\u91cf\u58f0\u660e\u4f4d\u7f6e\u4e2d\u95f4\u7684\u90e8\u5206\uff0c\u90fd\u662f\u8be5let\u53d8\u91cf\u7684\u6b7b\u533a\uff0c\u5728\u6b7b\u533a\u4e2d\uff0c\u7981\u6b62\u8bbf\u95ee\u8be5\u53d8\u91cf\u3002\u7531\u6b64\uff0c\u6211\u4eec\u7ed9\u51fa\u7ed3\u8bba\uff0c<strong>let\u58f0\u660e\u7684\u53d8\u91cf\u5b58\u5728\u53d8\u91cf\u63d0\u5347\uff0c \u4f46\u662f\u7531\u4e8e\u6b7b\u533a\u6211\u4eec\u65e0\u6cd5\u5728\u58f0\u660e\u524d\u8bbf\u95ee\u8fd9\u4e2a\u53d8\u91cf\u3002<\/strong><\/p>\n\n\n\n<p><strong>3. let \u7981\u6b62\u91cd\u590d\u58f0\u660e\u53d8\u91cf<\/strong><\/p>\n\n\n\n<p>\u4f7f\u7528 var \u53ef\u4ee5\u91cd\u590d\u58f0\u660e\u53d8\u91cf\uff0c\u4f46\u662f let \u4e0d\u5141\u8bb8\u5728\u540c\u4e00\u5757\u4f5c\u7528\u57df\u5185\u91cd\u590d\u58f0\u660e\u540c\u4e00\u4e2a\u53d8\u91cf\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>function fn (){\n   var a = 1;\n   let a = 2;\n   console.log(a); \/\/SyntaxError\n}\n\nfunction fn (){\n   let a = 1;\n   let a = 2;\n   console.log(a); \/\/SyntaxError\n}\n\nfunction fn (a){\n   let a = 2;\n   console.log(a); \/\/SyntaxError\n}<\/code><\/pre>\n\n\n\n<p>\u4e0a\u8ff0\u4ee3\u7801\u4f1a\u62a5\u8bed\u6cd5\u9519\u8bef\uff1b<\/p>\n\n\n\n<p><strong>4. let\u4e0d\u4f1a\u6210\u4e3a\u5168\u5c40\u5bf9\u8c61\u7684\u5c5e\u6027<\/strong><\/p>\n\n\n\n<p>\u6211\u4eec\u5728\u5168\u5c40\u8303\u56f4\u5185\u58f0\u660e\u4e00\u4e2a\u53d8\u91cf\u65f6\uff0c\u8fd9\u4e2a\u53d8\u91cf\u81ea\u52a8\u6210\u4e3a\u5168\u5c40\u5bf9\u8c61\u7684\u5c5e\u6027\uff08\u5728\u6d4f\u89c8\u5668\u548c<code>Node.js<\/code>\u73af\u5883\u4e0b\uff0c\u8fd9\u4e2a\u5168\u5c40\u5bf9\u8c61\u5206\u522b\u662f<code>window<\/code>\u548c<code>global<\/code>)\uff0c\u4f46let\u662f\u72ec\u7acb\u5b58\u5728\u7684\u53d8\u91cf\uff0c\u4e0d\u4f1a\u6210\u4e3a\u5168\u5c40\u5bf9\u8c61\u7684\u5c5e\u6027\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>var a = 1;\nconsole.log(window.a); \/\/1\n\nlet b = 2;\nconsole.log(window.b); \/\/ undefined<\/code><\/pre>\n\n\n\n<p><strong>5. const \u58f0\u660e\u7684\u5e38\u91cf<\/strong><\/p>\n\n\n\n<p>\u4ee5\u4e0a let \u7684\u89c4\u5219\u540c\u6837\u9002\u7528\u4e8e const\uff0c\u4f46\u662f\u8ddf let \u7684\u533a\u522b\u662f const \u58f0\u660e\u7684\u53d8\u91cf\u4e0d\u80fd\u91cd\u65b0\u8d4b\u503c\uff0c\u6240\u4ee5<strong>&nbsp;const \u58f0\u660e\u7684\u53d8\u91cf\u5fc5\u987b\u7ecf\u8fc7\u521d\u59cb\u5316<\/strong>\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const a = 1;\n\na = 2; \/\/ \/\/ Uncaught TypeError: Assignment to constant variable\nconst b; \/\/ Uncaught SyntaxError: Missing initializer in const declaration<\/code><\/pre>\n\n\n\n<p><strong>6. \u5757\u7ea7\u4f5c\u7528\u57df\u5b9e\u4f8b<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;p class=\"myp\"&gt;aaaa&lt;\/p&gt;\n&lt;p class=\"myp\"&gt;bbbb&lt;\/p&gt;\n&lt;p class=\"myp\"&gt;cccc&lt;\/p&gt;<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>objs=document.getElementsByClassName('myp');\ns=objs.length;\nfor(let i=0;i&lt;s;i++){\n\tobjs&#91;i].onclick=function(){\n\t\talert(i);\n\t}\n}<\/code><\/pre>\n\n\n\n<p>es6\u4e4b\u524d\u7684\u4f20\u7edfjs\u8fd9\u5757\u5b9e\u73b0\u4f1a\u6bd4\u8f83\u590d\u6742\uff0c\u800c\u4e14\u70b9\u51fb\u540e\u62a5\u9519\uff0c\u73b0\u5728\u7528\u5757\u7ea7let\u70b9\u51fb\u6bcf\u4e00\u4e2ap\u6807\u7b7e\u540e\u53ef\u6b63\u5e38\u663e\u793ai\u53d8\u91cf.<\/p>\n\n\n\n<p>\u90e8\u5206\u5185\u5bb9\u5f15\u7528\u81ea\uff1ahttps:\/\/segmentfault.com\/a\/1190000016491581 <\/p>\n","protected":false},"excerpt":{"rendered":"<p>var \u548c let \u7684\u533a\u522b\u662f\u8001\u751f\u5e38\u8c08\uff0c\u770b\u5230\u7f51\u4e0a\u4e00\u4e9b\u6587\u7ae0\u7684\u603b\u7ed3\uff0c\u6709\u7684\u4e0d\u592a\u5168\u9762\uff0c\u751a\u81f3\u6709\u7684\u63cf\u8ff0\u4e0d\u592a\u51c6\u786e\uff0c\u5728\u8fd9\u91cc\u5c3d\u91cf\u5168 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[4],"tags":[94,38,93,92],"_links":{"self":[{"href":"http:\/\/blog.yzmedu.com\/index.php?rest_route=\/wp\/v2\/posts\/555"}],"collection":[{"href":"http:\/\/blog.yzmedu.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/blog.yzmedu.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/blog.yzmedu.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/blog.yzmedu.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=555"}],"version-history":[{"count":4,"href":"http:\/\/blog.yzmedu.com\/index.php?rest_route=\/wp\/v2\/posts\/555\/revisions"}],"predecessor-version":[{"id":560,"href":"http:\/\/blog.yzmedu.com\/index.php?rest_route=\/wp\/v2\/posts\/555\/revisions\/560"}],"wp:attachment":[{"href":"http:\/\/blog.yzmedu.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=555"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/blog.yzmedu.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=555"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/blog.yzmedu.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=555"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}