{"id":149,"date":"2018-10-28T17:42:29","date_gmt":"2018-10-28T09:42:29","guid":{"rendered":"https:\/\/wp.winter-weng.cc\/work\/?p=149"},"modified":"2018-10-28T17:42:29","modified_gmt":"2018-10-28T09:42:29","slug":"js-es6-%e5%b8%b8%e7%94%a8%e8%aa%9e%e6%b3%95","status":"publish","type":"post","link":"https:\/\/wp.winter-weng.cc\/work\/?p=149","title":{"rendered":"[JS] ES6 const, let"},"content":{"rendered":"\n<p>ES6 \u53c8\u7a31\u70ba ECMAScript 2015\uff0c\u70ba JS \u63d0\u4f9b\u4e86\u4e00\u4e9b\u8a9e\u6cd5\u66f4\u65b0\u548c\u65b0\u7684\u8a9e\u6cd5\u7cd6\uff0c\u8b93\u958b\u767c\u8005\u53ef\u4ee5\u7528\u66f4\u77ed\u7684\u7a0b\u5f0f\u9054\u5230\u76f8\u540c\u7684\u529f\u80fd\u3002<\/p>\n\n\n<p>\u76ee\u524d ES6 \u5728\u700f\u89bd\u5668\u4e2d\u5c1a\u672a\u5b8c\u5168\u88ab\u652f\u63f4\uff0c\u56e0\u6b64\u9700\u8981\u900f\u904e Babel \u8f49\u8b6f\u56de ES5\uff0c\u8b93\u700f\u89bd\u5668\u53ef\u6b63\u5e38\u8fa8\u8b58\uff0c\u76f8\u95dc\u76f8\u5bb9\u6027\u53ef\u4ee5\u53c3\u8003\uff1a<a href=\"http:\/\/kangax.github.io\/compat-table\/es6\/\">http:\/\/kangax.github.io\/compat-table\/es6\/<\/a><\/p>\n\n\n<p>\u5e38\u7528\u7684 Feature \u53ef\u4ee5\u53c3\u8003\u9019\u500b\u7db2\u7ad9\uff1a<a href=\"http:\/\/es6-features.org\/\">http:\/\/es6-features.org\/<\/a>\uff0c\u9084\u6709\u7c21\u55ae\u660e\u77ad\u7684\u7bc4\u4f8b\u7c21\u76f4\u8d85\u8cbc\u5fc3\u3002<\/p>\n\n\n<hr class=\"wp-block-separator\" \/>\n\n\n<h4 class=\"wp-block-heading\">\u8b8a\u6578\u5ba3\u544a const, let<\/h4>\n\n\n<p>const \u7528\u4f86\u5ba3\u544a\u5e38\u6578\uff0c\u5e38\u6578\u662f\u4e0d\u53ef\u8b8a\u7684\uff0c\u56e0\u6b64\u82e5\u60f3\u8981\u66f4\u6539\u6703\u5831\u932f\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f Object \u6216 Array \u662f by reference\uff0c\u6240\u4ee5\u662f\u6307\u5411\u8a18\u61b6\u9ad4\u4f4d\u7f6e\uff0c\u56e0\u6b64\u82e5 push \u503c\u6216\u662f\u589e\u52a0\u7269\u4ef6\u5c6c\u6027\uff0c\u662f\u53ef\u4ee5\u6b63\u5e38\u904b\u4f5c\u7684\u3002<\/p>\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">const PI = 3.14;\nPI = 2;\n\/\/ Uncaught TypeError: Assignment to constant variable.\nconst person = {}\nperson.name = 'Winter';\n\/\/ person = {name: \"Winter\"}<\/pre>\n\n\n<p>\u4ee5\u5f80\u90fd\u4f7f\u7528 var \u4f86\u5ba3\u544a\u8b8a\u6578\uff0c\u4f46\u9019\u6a23\u5ba3\u544a\u51fa\u4f86\u7684\u8b8a\u6578\u662f\u5168\u57df\u7684\uff0c\u5982\u679c\u6c92\u7279\u5225\u88ab\u5305\u8d77\u4f86\uff0c\u5f88\u5bb9\u6613\u767c\u751f\u5728\u7a0b\u5f0f\u78bc\u4e2d\u56db\u8655\u5ba3\u544a\u5c0e\u81f4\u6c61\u67d3\u7684\u60c5\u6cc1\u3002ES6 \u65b0\u589e\u4e86 let \u95dc\u9375\u5b57\uff0c\u82e5\u7528 let \u5ba3\u544a\u8b8a\u6578\uff0c\u8b8a\u6578\u5c07\u6703\u662f Block-Scoped (\u584a\u7d1a\u4f5c\u7528\u57df)\uff0c\u7576\u4e00\u500b block \u7d50\u675f\u5c31\u6703\u81ea\u52d5\u6d88\u6ec5\u3002<\/p>\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">for (var i = 0; i &lt; 2; i++) {\n  console.log('for:' + i);\n}\nconsole.log('out:' + i);\n\/\/ out: 2\nfor (let i = 0; i &lt; 2; i++) {\n  console.log('for:' + i);\n}\nconsole.log('out:' + i);\n\/\/ out: undefined<\/pre>\n\n\n<p>\u5b98\u65b9\u5efa\u8b70\u80fd\u4f7f\u7528 let \u7684\u5730\u65b9\u5c31\u76e1\u91cf\u4f7f\u7528\uff0c\u53e6\u5916\u8209\u4e00\u500b\u6700\u5e38\u88ab\u62ff\u4f86\u8aaa\u660e\u9589\u5305 (Closure) \u7684\u4f8b\u5b50\uff0c\u5982\u679c\u6c92\u6709\u4f7f\u7528\u7acb\u5373\u51fd\u5f0f (IIFE, Immediately Invoked Function Expression) \u7d66\u4e88\u4fdd\u8b77\uff0c\u8b8a\u6578 i \u6703\u88ab for \u8ff4\u5708\u5f71\u97ff\uff0c\u5728 1 \u79d2\u5f8c\u5370\u51fa\u4e09\u6b21 2\u3002\u4f46\u5982\u679c\u4f7f\u7528 let \u7684\u8a71\uff0c\u4e00\u5207\u8b8a\u5f97\u5f88 Awesome\uff01<\/p>\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">for (var i = 0; i &lt; 2; i++) {\n  (function (i) {\n    window.setTimeout(function() {\n      console.log(i);\n    }, 1000)\n  })(i);\n}\n\/\/ \u4f7f\u7528 let\nfor (let i = 0; i &lt; 2; i++) {\n    window.setTimeout(function() {\n      console.log(i);\n    }, 1000)\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>ES6 \u53c8\u7a31\u70ba ECMAScript 2015\uff0c\u70ba&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"pgc_sgb_lightbox_settings":"","footnotes":""},"categories":[4],"tags":[18,21],"class_list":["post-149","post","type-post","status-publish","format-standard","hentry","category-javascript","tag-es6","tag-javascript"],"_links":{"self":[{"href":"https:\/\/wp.winter-weng.cc\/work\/index.php?rest_route=\/wp\/v2\/posts\/149","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wp.winter-weng.cc\/work\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wp.winter-weng.cc\/work\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wp.winter-weng.cc\/work\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/wp.winter-weng.cc\/work\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=149"}],"version-history":[{"count":0,"href":"https:\/\/wp.winter-weng.cc\/work\/index.php?rest_route=\/wp\/v2\/posts\/149\/revisions"}],"wp:attachment":[{"href":"https:\/\/wp.winter-weng.cc\/work\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=149"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wp.winter-weng.cc\/work\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=149"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wp.winter-weng.cc\/work\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=149"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}