{"id":199432,"date":"2020-09-07T09:28:58","date_gmt":"2020-09-07T01:28:58","guid":{"rendered":"https:\/\/gulass.cn\/?p=199432"},"modified":"2020-08-26T09:29:31","modified_gmt":"2020-08-26T01:29:31","slug":"es6-let-and","status":"publish","type":"post","link":"https:\/\/gulass.cn\/es6-let-and.html","title":{"rendered":"ES6 let \u4e0e const\u7684\u5e94\u7528\u4ecb\u7ecd"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\nES2015(ES6) \u65b0\u589e\u52a0\u4e86\u4e24\u4e2a\u91cd\u8981\u7684 JavaScript \u5173\u952e\u5b57: let \u548c const\u3002let \u58f0\u660e\u7684\u53d8\u91cf\u53ea\u5728 let \u547d\u4ee4\u6240\u5728\u7684\u4ee3\u7801\u5757\u5185\u6709\u6548\u3002const \u58f0\u660e\u4e00\u4e2a\u53ea\u8bfb\u7684\u5e38\u91cf\uff0c\u4e00\u65e6\u58f0\u660e\uff0c\u5e38\u91cf\u7684\u503c\u5c31\u4e0d\u80fd\u6539\u53d8\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\"\"<\/p>\n

let \u547d\u4ee4<\/strong><\/div>\n

\u57fa\u672c\u7528\u6cd5:<\/p>\n

{\r\n  let a = 0;\r\n  a   \/\/ 0\r\n}\r\na   \/\/ \u62a5\u9519 ReferenceError: a is not defined<\/pre>\n

\u4ee3\u7801\u5757\u5185\u6709\u6548<\/strong><\/p>\n

let \u662f\u5728\u4ee3\u7801\u5757\u5185\u6709\u6548\uff0cvar \u662f\u5728\u5168\u5c40\u8303\u56f4\u5185\u6709\u6548:<\/p>\n

{\r\n  let a = 0;\r\n  var b = 1;\r\n}\r\na  \/\/ ReferenceError: a is not defined\r\nb  \/\/ 1<\/pre>\n

\u4e0d\u80fd\u91cd\u590d\u58f0\u660e<\/strong><\/p>\n

let \u53ea\u80fd\u58f0\u660e\u4e00\u6b21 var \u53ef\u4ee5\u58f0\u660e\u591a\u6b21:<\/p>\n

let a = 1;\r\nlet a = 2;\r\nvar b = 3;\r\nvar b = 4;\r\na  \/\/ Identifier 'a' has already been declared\r\nb  \/\/ 4<\/pre>\n
\r\nfor \u5faa\u73af\u8ba1\u6570\u5668\u5f88\u9002\u5408\u7528 let\r\n\r\n<\/pre>\n
for (var i = 0; i < 10; i++) {\r\n  setTimeout(function(){\r\n    console.log(i);\r\n  })\r\n}\r\n\/\/ \u8f93\u51fa\u5341\u4e2a 10\r\nfor (let j = 0; j < 10; j++) {\r\n  setTimeout(function(){\r\n    console.log(j);\r\n  })\r\n}\r\n\/\/ \u8f93\u51fa 0123456789<\/pre>\n

\u53d8\u91cf i \u662f\u7528 var \u58f0\u660e\u7684\uff0c\u5728\u5168\u5c40\u8303\u56f4\u5185\u6709\u6548\uff0c\u6240\u4ee5\u5168\u5c40\u4e2d\u53ea\u6709\u4e00\u4e2a\u53d8\u91cf i, \u6bcf\u6b21\u5faa\u73af\u65f6\uff0csetTimeout \u5b9a\u65f6\u5668\u91cc\u9762\u7684 i \u6307\u7684\u662f\u5168\u5c40\u53d8\u91cf i \uff0c\u800c\u5faa\u73af\u91cc\u7684\u5341\u4e2a setTimeout \u662f\u5728\u5faa\u73af\u7ed3\u675f\u540e\u624d\u6267\u884c\uff0c\u6240\u4ee5\u6b64\u65f6\u7684 i \u90fd\u662f 10\u3002<\/p>\n

\u53d8\u91cf j \u662f\u7528 let \u58f0\u660e\u7684\uff0c\u5f53\u524d\u7684 j \u53ea\u5728\u672c\u8f6e\u5faa\u73af\u4e2d\u6709\u6548\uff0c\u6bcf\u6b21\u5faa\u73af\u7684 j \u5176\u5b9e\u90fd\u662f\u4e00\u4e2a\u65b0\u7684\u53d8\u91cf\uff0c\u6240\u4ee5 setTimeout \u5b9a\u65f6\u5668\u91cc\u9762\u7684 j \u5176\u5b9e\u662f\u4e0d\u540c\u7684\u53d8\u91cf\uff0c\u5373\u6700\u540e\u8f93\u51fa 12345\u3002\uff08\u82e5\u6bcf\u6b21\u5faa\u73af\u7684\u53d8\u91cf j \u90fd\u662f\u91cd\u65b0\u58f0\u660e\u7684\uff0c\u5982\u4f55\u77e5\u9053\u524d\u4e00\u4e2a\u5faa\u73af\u7684\u503c\uff1f\u8fd9\u662f\u56e0\u4e3a JavaScript \u5f15\u64ce\u5185\u90e8\u4f1a\u8bb0\u4f4f\u524d\u4e00\u4e2a\u5faa\u73af\u7684\u503c\uff09\u3002<\/p>\n

\u4e0d\u5b58\u5728\u53d8\u91cf\u63d0\u5347<\/strong><\/span><\/div>\n

let \u4e0d\u5b58\u5728\u53d8\u91cf\u63d0\u5347\uff0cvar \u4f1a\u53d8\u91cf\u63d0\u5347:<\/p>\n

console.log(a);  \/\/ReferenceError: a is not defined\r\nlet a = \"apple\";\r\n \r\nconsole.log(b);  \/\/undefined\r\nvar b = \"banana\";<\/pre>\n

\u53d8\u91cf b \u7528 var \u58f0\u660e\u5b58\u5728\u53d8\u91cf\u63d0\u5347\uff0c\u6240\u4ee5\u5f53\u811a\u672c\u5f00\u59cb\u8fd0\u884c\u7684\u65f6\u5019\uff0cb \u5df2\u7ecf\u5b58\u5728\u4e86\uff0c\u4f46\u662f\u8fd8\u6ca1\u6709\u8d4b\u503c\uff0c\u6240\u4ee5\u4f1a\u8f93\u51fa undefined\u3002<\/p>\n

\u53d8\u91cf a \u7528 let \u58f0\u660e\u4e0d\u5b58\u5728\u53d8\u91cf\u63d0\u5347\uff0c\u5728\u58f0\u660e\u53d8\u91cf a \u4e4b\u524d\uff0ca \u4e0d\u5b58\u5728\uff0c\u6240\u4ee5\u4f1a\u62a5\u9519\u3002<\/p>\n

const \u547d\u4ee4<\/strong><\/div>\n

const \u58f0\u660e\u4e00\u4e2a\u53ea\u8bfb\u53d8\u91cf\uff0c\u58f0\u660e\u4e4b\u540e\u4e0d\u5141\u8bb8\u6539\u53d8\u3002\u610f\u5473\u7740\uff0c\u4e00\u65e6\u58f0\u660e\u5fc5\u987b\u521d\u59cb\u5316\uff0c\u5426\u5219\u4f1a\u62a5\u9519\u3002<\/p>\n

\u57fa\u672c\u7528\u6cd5:<\/p>\n

const PI = \"3.1415926\";\r\nPI  \/\/ 3.1415926\r\n\r\nconst MY_AGE;  \/\/ SyntaxError: Missing initializer in const declaration<\/pre>\n

\u6682\u65f6\u6027\u6b7b\u533a:<\/p>\n

var PI = \"a\";\r\nif(true){\r\n  console.log(PI);  \/\/ ReferenceError: PI is not defined\r\n  const PI = \"3.1415926\";\r\n}<\/pre>\n

ES6 \u660e\u786e\u89c4\u5b9a\uff0c\u4ee3\u7801\u5757\u5185\u5982\u679c\u5b58\u5728 let \u6216\u8005 const\uff0c\u4ee3\u7801\u5757\u4f1a\u5bf9\u8fd9\u4e9b\u547d\u4ee4\u58f0\u660e\u7684\u53d8\u91cf\u4ece\u5757\u7684\u5f00\u59cb\u5c31\u5f62\u6210\u4e00\u4e2a\u5c01\u95ed\u4f5c\u7528\u57df\u3002\u4ee3\u7801\u5757\u5185\uff0c\u5728\u58f0\u660e\u53d8\u91cf PI \u4e4b\u524d\u4f7f\u7528\u5b83\u4f1a\u62a5\u9519\u3002<\/p>\n

\u6ce8\u610f\u8981\u70b9<\/strong><\/div>\n

const \u5982\u4f55\u505a\u5230\u53d8\u91cf\u5728\u58f0\u660e\u521d\u59cb\u5316\u4e4b\u540e\u4e0d\u5141\u8bb8\u6539\u53d8\u7684\uff1f\u5176\u5b9e const \u5176\u5b9e\u4fdd\u8bc1\u7684\u4e0d\u662f\u53d8\u91cf\u7684\u503c\u4e0d\u53d8\uff0c\u800c\u662f\u4fdd\u8bc1\u53d8\u91cf\u6307\u5411\u7684\u5185\u5b58\u5730\u5740\u6240\u4fdd\u5b58\u7684\u6570\u636e\u4e0d\u5141\u8bb8\u6539\u52a8\u3002\u6b64\u65f6\uff0c\u4f60\u53ef\u80fd\u5df2\u7ecf\u60f3\u5230\uff0c\u7b80\u5355\u7c7b\u578b\u548c\u590d\u5408\u7c7b\u578b\u4fdd\u5b58\u503c\u7684\u65b9\u5f0f\u662f\u4e0d\u540c\u7684\u3002\u662f\u7684\uff0c\u5bf9\u4e8e\u7b80\u5355\u7c7b\u578b\uff08\u6570\u503c number\u3001\u5b57\u7b26\u4e32 string \u3001\u5e03\u5c14\u503c boolean\uff09,\u503c\u5c31\u4fdd\u5b58\u5728\u53d8\u91cf\u6307\u5411\u7684\u90a3\u4e2a\u5185\u5b58\u5730\u5740\uff0c\u56e0\u6b64 const \u58f0\u660e\u7684\u7b80\u5355\u7c7b\u578b\u53d8\u91cf\u7b49\u540c\u4e8e\u5e38\u91cf\u3002\u800c\u590d\u6742\u7c7b\u578b\uff08\u5bf9\u8c61 object\uff0c\u6570\u7ec4 array\uff0c\u51fd\u6570 function\uff09\uff0c\u53d8\u91cf\u6307\u5411\u7684\u5185\u5b58\u5730\u5740\u5176\u5b9e\u662f\u4fdd\u5b58\u4e86\u4e00\u4e2a\u6307\u5411\u5b9e\u9645\u6570\u636e\u7684\u6307\u9488\uff0c\u6240\u4ee5 const \u53ea\u80fd\u4fdd\u8bc1\u6307\u9488\u662f\u56fa\u5b9a\u7684\uff0c\u81f3\u4e8e\u6307\u9488\u6307\u5411\u7684\u6570\u636e\u7ed3\u6784\u53d8\u4e0d\u53d8\u5c31\u65e0\u6cd5\u63a7\u5236\u4e86\uff0c\u6240\u4ee5\u4f7f\u7528 const \u58f0\u660e\u590d\u6742\u7c7b\u578b\u5bf9\u8c61\u65f6\u8981\u614e\u91cd\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"

\u57fa\u672c\u7528\u6cd5: { let a = 0; a \/\/ 0 } a \/\/ \u62a5\u9519 ReferenceError: a i […]<\/p>\n","protected":false},"author":321,"featured_media":199434,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-199432","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/199432","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/users\/321"}],"replies":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/comments?post=199432"}],"version-history":[{"count":2,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/199432\/revisions"}],"predecessor-version":[{"id":199440,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/199432\/revisions\/199440"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/media\/199434"}],"wp:attachment":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/media?parent=199432"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/categories?post=199432"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/tags?post=199432"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}