{"id":727,"date":"2020-10-16T16:22:57","date_gmt":"2020-10-16T08:22:57","guid":{"rendered":"http:\/\/blog.nami.idv.tw\/?p=727"},"modified":"2020-10-16T16:22:57","modified_gmt":"2020-10-16T08:22:57","slug":"strange-javascript-variable-scope-issue","status":"publish","type":"post","link":"https:\/\/blog.nami.idv.tw\/?p=727","title":{"rendered":"Strange javascript variable scope issue"},"content":{"rendered":"\n<p>\u6628\u5929\u5728\u6e2c\u8a66\u00a0Web App \u65bc iPad \u4e0a\u904b\u4f5c\u72c0\u6cc1\u6642\u767c\u751f\u4e00\u500b\u610f\u5916\u7684\u72c0\u6cc1\uff0c\u76f8\u540c\u7684\u7a0b\u5f0f\u5728 Windows \u7684 Google Chrome, Mozilla Firefox \u90fd\u6b63\u5e38\u904b\u4f5c\uff0c\u4f46\u5230\u4e86 iPad \u4e0a Apple Safari, Google Chrome, Mozilla Firefox \u537b\u5168\u6578\u639b\u6389\uff0c\u7d93\u904e debug \u5f8c\u767c\u73fe\u4e00\u500b\u5947\u7279\u7684\u72c0\u6cc1\uff1a\u8b8a\u6578\u7684 scope \u5728\u4e0d\u540c\u74b0\u5883(Browsers on Windows vs Browsers on iOS) \u5c45\u7136\u6709\u4e0d\u540c\u7bc4\u570d!?<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: xml; auto-links: false; title: ; notranslate\" title=\"\">\n&lt;!-- \u7c21\u5316\u793a\u610f --&gt;\n&lt;script defer&gt;const foo={ &#039;bar&#039;: true };&lt;\/script&gt;\n&lt;script src=&quot;app.js&quot; type=&quot;module&quot;&gt;&lt;\/script&gt;\n<\/pre><\/div>\n\n\n<p>\u4ee5\u4e0a\u540c\u6a23\u7684\u7a0b\u5f0f\u78bc\u5728 Windows \u4e0b\u7684\u5169\u500b\u700f\u89bd\u5668\uff0capp.js \u90fd\u80fd\u5b58\u53d6\u5230\u8b8a\u6578 foo\uff0c\u4f46\u5230\u4e86 iOS \u4e0a\u9762\uff0c\u4e09\u500b\u700f\u89bd\u5668\u90fd\u62cb\u51fa undefined\uff0c\u56e0\u6b64\u76f4\u89ba\u5c31\u5224\u5b9a\u662f variable scope \u7684\u554f\u984c\uff0c\u6240\u4ee5\u5c07 foo \u5ba3\u544a\u7531 const foo \u6539\u6210 window.foo\uff0c\u5c07\u5176\u9644\u52a0\u5230 window \u7269\u4ef6\u5f8c app.js \u5c31\u80fd\u6b63\u5e38\u5730\u76f4\u63a5\u4ee5\u8b8a\u6578\u540d\u7a31\u00a0foo\u00a0\u5b58\u53d6(\u4e0d\u9700\u8981\u4ee5 window.foo)\uff0c\u82e5\u662f module \u8f09\u5165\u7684 js \u4e0d\u80fd\u76f4\u63a5\u5b58\u53d6 foo \u7684\u8a71\u4e26\u6c92\u6709\u4ec0\u9ebc\u554f\u984c (\u975e\u5168\u57df\u8b8a\u6578)\uff0c\u4f46\u4e0d\u540c\u5e73\u53f0\u7d50\u679c\u4e0d\u540c\u9019\u5c31\u6709\u4e9b\u554f\u984c\u4e86\u2026<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6628\u5929\u5728\u6e2c\u8a66\u00a0Web App \u65bc iPad \u4e0a\u904b\u4f5c\u72c0\u6cc1\u6642\u767c\u751f\u4e00\u500b\u610f\u5916\u7684\u72c0\u6cc1\uff0c\u76f8\u540c\u7684 &hellip; <a href=\"https:\/\/blog.nami.idv.tw\/?p=727\">\u95b1\u8b80\u5168\u6587 <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[17,33],"tags":[12],"class_list":["post-727","post","type-post","status-publish","format-standard","hentry","category-troubleshooting","category-develop","tag-javascript"],"_links":{"self":[{"href":"https:\/\/blog.nami.idv.tw\/index.php?rest_route=\/wp\/v2\/posts\/727","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.nami.idv.tw\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.nami.idv.tw\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.nami.idv.tw\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.nami.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=727"}],"version-history":[{"count":3,"href":"https:\/\/blog.nami.idv.tw\/index.php?rest_route=\/wp\/v2\/posts\/727\/revisions"}],"predecessor-version":[{"id":730,"href":"https:\/\/blog.nami.idv.tw\/index.php?rest_route=\/wp\/v2\/posts\/727\/revisions\/730"}],"wp:attachment":[{"href":"https:\/\/blog.nami.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=727"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.nami.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=727"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.nami.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=727"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}