{"id":2075,"date":"2025-02-26T23:35:40","date_gmt":"2025-02-26T15:35:40","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=2075"},"modified":"2025-02-26T23:35:40","modified_gmt":"2025-02-26T15:35:40","slug":"javascript-let-%e5%92%8c-const","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/02\/26\/javascript-let-%e5%92%8c-const\/","title":{"rendered":"JavaScript let \u548c const"},"content":{"rendered":"\n<p>\u5728 JavaScript \u4e2d\uff0c<code>let<\/code> \u548c <code>const<\/code> \u662f\u7528\u6765\u58f0\u660e\u53d8\u91cf\u7684\u5173\u952e\u5b57\uff0c\u5b83\u4eec\u662f\u5728 ES6\uff08ECMAScript 2015\uff09\u4e2d\u5f15\u5165\u7684\uff0c\u65e8\u5728\u89e3\u51b3 <code>var<\/code> \u7684\u4e00\u4e9b\u95ee\u9898\u3002\u5c3d\u7ba1\u5b83\u4eec\u90fd\u7528\u4e8e\u58f0\u660e\u53d8\u91cf\uff0c\u4f46\u5728\u4f7f\u7528\u4e0a\u6709\u4e00\u4e9b\u5173\u952e\u7684\u533a\u522b\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>1. <code>let<\/code> \u5173\u952e\u5b57<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u4f5c\u7528\u57df\uff1a<\/strong> <code>let<\/code> \u58f0\u660e\u7684\u53d8\u91cf\u5177\u6709\u5757\u7ea7\u4f5c\u7528\u57df\u3002\u5b83\u4ec5\u5728\u5176\u58f0\u660e\u6240\u5728\u7684\u4ee3\u7801\u5757\u5185\u6709\u6548\uff0c\u800c\u4e0d\u50cf <code>var<\/code> \u90a3\u6837\u5177\u6709\u51fd\u6570\u4f5c\u7528\u57df\u3002<\/li>\n\n\n\n<li><strong>\u91cd\u65b0\u8d4b\u503c\uff1a<\/strong> \u4f7f\u7528 <code>let<\/code> \u58f0\u660e\u7684\u53d8\u91cf\u53ef\u4ee5\u88ab\u91cd\u65b0\u8d4b\u503c\u3002<\/li>\n\n\n\n<li><strong>\u58f0\u660e\u63d0\u5347\uff1a<\/strong> <code>let<\/code> \u4e5f\u4f1a\u53d1\u751f\u63d0\u5347\uff08hoisting\uff09\uff0c\u4f46\u4e0d\u4f1a\u521d\u59cb\u5316\u3002\u5373\u4f7f\u5b83\u88ab\u63d0\u5347\u5230\u4f5c\u7528\u57df\u9876\u90e8\uff0c\u4e0d\u80fd\u5728\u58f0\u660e\u4e4b\u524d\u8bbf\u95ee\uff08\u8fd9\u79f0\u4e3a\u201c\u6682\u65f6\u6027\u6b7b\u533a\u201d\uff09\u3002<\/li>\n<\/ul>\n\n\n\n<h5 class=\"wp-block-heading\"><strong>\u793a\u4f8b\uff1a<\/strong><\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>let a = 10;\na = 20; \/\/ \u53ef\u4ee5\u91cd\u65b0\u8d4b\u503c\nconsole.log(a); \/\/ \u8f93\u51fa 20\n\nif (true) {\n  let b = 30; \/\/ b \u53ea\u5728 if \u5757\u5185\u6709\u6548\n  console.log(b); \/\/ \u8f93\u51fa 30\n}\n\nconsole.log(b); \/\/ \u4f1a\u62a5\u9519 ReferenceError: b is not defined<\/code><\/pre>\n\n\n\n<p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c<code>b<\/code> \u53ea\u5728 <code>if<\/code> \u5757\u5185\u90e8\u6709\u6548\uff0c\u51fa\u4e86\u8fd9\u4e2a\u5757\uff0c<code>b<\/code> \u4e0d\u518d\u6709\u6548\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2. <code>const<\/code> \u5173\u952e\u5b57<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u4f5c\u7528\u57df\uff1a<\/strong> \u4e0e <code>let<\/code> \u76f8\u540c\uff0c<code>const<\/code> \u4e5f\u5177\u6709\u5757\u7ea7\u4f5c\u7528\u57df\u3002<\/li>\n\n\n\n<li><strong>\u4e0d\u53ef\u91cd\u65b0\u8d4b\u503c\uff1a<\/strong> \u4f7f\u7528 <code>const<\/code> \u58f0\u660e\u7684\u53d8\u91cf\u5fc5\u987b\u5728\u58f0\u660e\u65f6\u521d\u59cb\u5316\uff0c\u5e76\u4e14\u5176\u503c\u5728\u540e\u7eed\u4e0d\u80fd\u88ab\u91cd\u65b0\u8d4b\u503c\u3002<code>const<\/code> \u58f0\u660e\u7684\u662f\u5e38\u91cf\u3002<\/li>\n\n\n\n<li><strong>\u58f0\u660e\u63d0\u5347\uff1a<\/strong> \u4e0e <code>let<\/code> \u7c7b\u4f3c\uff0c<code>const<\/code> \u4e5f\u4f1a\u53d1\u751f\u63d0\u5347\uff0c\u5e76\u4e14\u4e0d\u80fd\u5728\u58f0\u660e\u4e4b\u524d\u8bbf\u95ee\u3002<\/li>\n\n\n\n<li><strong>\u5e38\u91cf\u5f15\u7528\uff1a<\/strong> <code>const<\/code> \u58f0\u660e\u7684\u53d8\u91cf\u672c\u8eab\u662f\u4e0d\u53ef\u6539\u53d8\u7684\uff0c\u4f46\u5982\u679c\u5b83\u662f\u5bf9\u8c61\u6216\u6570\u7ec4\uff0c\u5bf9\u8c61\u7684\u5c5e\u6027\u6216\u6570\u7ec4\u7684\u5143\u7d20\u662f\u53ef\u4ee5\u88ab\u4fee\u6539\u7684\u3002<\/li>\n<\/ul>\n\n\n\n<h5 class=\"wp-block-heading\"><strong>\u793a\u4f8b\uff1a<\/strong><\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>const pi = 3.14;\nconsole.log(pi); \/\/ \u8f93\u51fa 3.14\n\n\/\/ pi = 3.14159; \/\/ \u4f1a\u62a5\u9519 TypeError: Assignment to constant variable\n\nconst obj = { name: \"Alice\" };\nobj.name = \"Bob\"; \/\/ \u53ef\u4ee5\u4fee\u6539\u5bf9\u8c61\u7684\u5c5e\u6027\nconsole.log(obj.name); \/\/ \u8f93\u51fa \"Bob\"\n\n\/\/ obj = {}; \/\/ \u4f1a\u62a5\u9519 TypeError: Assignment to constant variable<\/code><\/pre>\n\n\n\n<p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c<code>pi<\/code> \u7684\u503c\u4e0d\u80fd\u88ab\u6539\u53d8\uff0c\u4f46\u5bf9\u8c61 <code>obj<\/code> \u7684\u5c5e\u6027 <code>name<\/code> \u53ef\u4ee5\u88ab\u4fee\u6539\uff0c\u56e0\u4e3a <code>const<\/code> \u53ea\u662f\u786e\u4fdd\u53d8\u91cf\u672c\u8eab\u4e0d\u80fd\u88ab\u91cd\u65b0\u8d4b\u503c\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>3. <code>let<\/code> \u548c <code>const<\/code> \u4e0e <code>var<\/code> \u7684\u533a\u522b<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u4f5c\u7528\u57df\uff1a<\/strong> <code>var<\/code> \u58f0\u660e\u7684\u53d8\u91cf\u5177\u6709\u51fd\u6570\u4f5c\u7528\u57df\uff0c\u800c <code>let<\/code> \u548c <code>const<\/code> \u5177\u6709\u5757\u7ea7\u4f5c\u7528\u57df\u3002<\/li>\n\n\n\n<li><strong>\u91cd\u590d\u58f0\u660e\uff1a<\/strong> \u4f7f\u7528 <code>var<\/code> \u53ef\u4ee5\u5728\u540c\u4e00\u4e2a\u4f5c\u7528\u57df\u4e2d\u591a\u6b21\u58f0\u660e\u540c\u4e00\u4e2a\u53d8\u91cf\uff0c\u4f46 <code>let<\/code> \u548c <code>const<\/code> \u4e0d\u5141\u8bb8\u5728\u540c\u4e00\u4f5c\u7528\u57df\u4e2d\u91cd\u590d\u58f0\u660e\u53d8\u91cf\u3002<\/li>\n\n\n\n<li><strong>\u63d0\u5347\uff1a<\/strong> <code>var<\/code> \u4f1a\u88ab\u63d0\u5347\u5e76\u521d\u59cb\u5316\u4e3a <code>undefined<\/code>\uff0c\u800c <code>let<\/code> \u548c <code>const<\/code> \u4f1a\u63d0\u5347\uff0c\u4f46\u4e0d\u4f1a\u521d\u59cb\u5316\uff0c\u5bfc\u81f4\u5728\u58f0\u660e\u4e4b\u524d\u4f7f\u7528\u4f1a\u51fa\u73b0\u9519\u8bef\u3002<\/li>\n<\/ul>\n\n\n\n<h5 class=\"wp-block-heading\"><strong>\u793a\u4f8b\uff1a<\/strong><\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ var\nvar x = 10;\nvar x = 20; \/\/ \u6ca1\u6709\u62a5\u9519\uff0c\u53ef\u4ee5\u91cd\u65b0\u58f0\u660e\nconsole.log(x); \/\/ \u8f93\u51fa 20\n\n\/\/ let\nlet y = 10;\nlet y = 20; \/\/ \u4f1a\u62a5\u9519 SyntaxError: Identifier 'y' has already been declared\n\n\/\/ const\nconst z = 10;\nconst z = 20; \/\/ \u4f1a\u62a5\u9519 SyntaxError: Identifier 'z' has already been declared<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>4. \u4f55\u65f6\u4f7f\u7528 <code>let<\/code> \u548c <code>const<\/code><\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u4f7f\u7528 <code>let<\/code><\/strong>\uff1a\u5f53\u4f60\u9700\u8981\u5728\u4ee3\u7801\u4e2d\u91cd\u65b0\u8d4b\u503c\u4e00\u4e2a\u53d8\u91cf\u65f6\uff0c\u4f7f\u7528 <code>let<\/code>\u3002<\/li>\n\n\n\n<li><strong>\u4f7f\u7528 <code>const<\/code><\/strong>\uff1a\u5f53\u4f60\u58f0\u660e\u4e00\u4e2a\u5e38\u91cf\uff0c\u5e76\u4e14\u8be5\u5e38\u91cf\u7684\u503c\u4e0d\u5e94\u8be5\u88ab\u6539\u53d8\u65f6\uff0c\u4f7f\u7528 <code>const<\/code>\u3002\u5c24\u5176\u662f\u5f53\u4f60\u58f0\u660e\u5bf9\u8c61\u3001\u6570\u7ec4\u7b49\u5f15\u7528\u7c7b\u578b\u65f6\uff0c\u5982\u679c\u5f15\u7528\u672c\u8eab\u4e0d\u4f1a\u6539\u53d8\uff0c\u4f7f\u7528 <code>const<\/code> \u4e5f\u662f\u4e00\u79cd\u597d\u4e60\u60ef\u3002<\/li>\n<\/ul>\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><code>let<\/code> \u7528\u4e8e\u58f0\u660e\u53ef\u4ee5\u4fee\u6539\u7684\u53d8\u91cf\uff0c\u5177\u6709\u5757\u7ea7\u4f5c\u7528\u57df\u3002<\/li>\n\n\n\n<li><code>const<\/code> \u7528\u4e8e\u58f0\u660e\u5e38\u91cf\uff0c\u58f0\u660e\u65f6\u5fc5\u987b\u521d\u59cb\u5316\uff0c\u4e14\u65e0\u6cd5\u91cd\u65b0\u8d4b\u503c\u3002<\/li>\n\n\n\n<li><code>let<\/code> \u548c <code>const<\/code> \u90fd\u4e0d\u4f1a\u53d1\u751f\u53d8\u91cf\u63d0\u5347\uff08\u53d8\u91cf\u63d0\u5347\u4f1a\u628a\u58f0\u660e\u63d0\u5347\u5230\u4ee3\u7801\u9876\u90e8\uff0c\u4f46\u4e0d\u4f1a\u8d4b\u503c\uff09\uff0c\u4ece\u800c\u907f\u514d\u4e86 <code>var<\/code> \u5f15\u8d77\u7684\u6f5c\u5728\u95ee\u9898\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u7406\u89e3 <code>let<\/code> \u548c <code>const<\/code> \u7684\u5dee\u5f02\u5e76\u5728\u9002\u5f53\u7684\u573a\u666f\u4e2d\u4f7f\u7528\u5b83\u4eec\uff0c\u53ef\u4ee5\u5e2e\u52a9\u4f60\u7f16\u5199\u66f4\u6e05\u6670\u548c\u5b89\u5168\u7684 JavaScript \u4ee3\u7801\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 JavaScript \u4e2d\uff0clet \u548c const \u662f\u7528\u6765\u58f0\u660e\u53d8\u91cf\u7684\u5173\u952e\u5b57\uff0c\u5b83\u4eec\u662f\u5728 ES6\uff08ECMAScr [&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-2075","post","type-post","status-publish","format-standard","hentry","category-javascript"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2075","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=2075"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2075\/revisions"}],"predecessor-version":[{"id":2076,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2075\/revisions\/2076"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=2075"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=2075"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=2075"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}