{"id":1112,"date":"2022-04-28T22:03:09","date_gmt":"2022-04-28T14:03:09","guid":{"rendered":"http:\/\/zhuxinyong.com\/?p=1112"},"modified":"2023-02-16T14:40:36","modified_gmt":"2023-02-16T06:40:36","slug":"12-javascript-zhong-de-zuo-yong-yu","status":"publish","type":"post","link":"https:\/\/zhuxinyong.com\/?p=1112","title":{"rendered":"12 &#8211; JavaScript \u4e2d\u7684\u4f5c\u7528\u57df"},"content":{"rendered":"<p><img decoding=\"async\" src=\"http:\/\/zhuxinyong.com\/wp-content\/uploads\/2022\/04\/16511548658759.jpg\" alt=\"\" \/><\/p>\n<p>\u539f\u6587\u5730\u5740\uff1a<a href=\"https:\/\/dev.to\/bhagatparwinder\/scope-in-javascript-3jim\" target=\"_blank\" rel=\"noopener\">https:\/\/dev.to\/bhagatparwinder\/scope-in-javascript-3jim<\/a><\/p>\n<p>JavaScript \u4e2d\u7684\u4f5c\u7528\u57df\u89c4\u5b9a\u4e86\u4e00\u4e2a\u53d8\u91cf\u6216\u51fd\u6570\u7684\u53ef\u7528\u8303\u56f4\u3002\u6839\u636e\u53d8\u91cf\u6216\u51fd\u6570\u7684\u58f0\u660e\u4f4d\u7f6e\u5b83\u4eec\u6216\u8bb8\u53ea\u80fd\u5728\u4e2a\u522b JavaScript \u4ee3\u7801\u5757\u4e2d\u53ef\u7528\u5728\u5176\u4ed6\u5730\u65b9\u5219\u4e0d\u884c\u3002\u6211\u4eec\u5728 JavaScript \u4e2d\u4f1a\u63a5\u89e6\u5230\u4e09\u79cd\u4f5c\u7528\u57df\u3002<\/p>\n<h3><a id=\"global-scope\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>Global Scope<\/h3>\n<p>\u5f53\u4e00\u4e2a\u53d8\u91cf\/\u65b9\u6cd5\u5728 script \u6216 JS \u6587\u4ef6\u4e2d\uff08\u4e0d\u5c5e\u4e8e\u4efb\u4f55\u4e00\u4e2a\u51fd\u6570\uff09\u58f0\u660e \u5e76\u4e14\u53ef\u4ee5\u90a3\u4e2a\u6587\u4ef6\u6216\u8005 HTML \u6587\u6863\u4e2d\u5f15\u7528\u5230\uff0c\u5b83\u5c31\u662f\u88ab\u58f0\u660e\u5728\u4e86\u5168\u5c40\u4f5c\u7528\u57df\u3002\u5b83\u4e0d\u5c5e\u4e8e\u67d0\u4e2a\u65b9\u6cd5\u6216\u6a21\u5757\u3002<\/p>\n<p>\u5728\u6d4f\u89c8\u5668\u4e2d\uff0c\u6709\u4e00\u4e2a\u5168\u5c40\u53d8\u91cf\u53eb  <strong>window<\/strong>\uff0c\u4f60\u53ef\u80fd\u4f1a\u770b\u5230\u5f00\u53d1\u8005\u7ecf\u5e38\u628a <strong>window.setInterval<\/strong> \u548c <strong>setInterval<\/strong> \u4ea4\u6362\u4f7f\u7528\u800c\u6ca1\u6709\u5e26\u6765\u540e\u679c\u6216\u526f\u4f5c\u7528\uff0c\u90a3\u662f\u56e0\u4e3a setInterval \u5728\u5168\u5c40\u4f5c\u7528\u57df\u4e2d\u3002\u6d4f\u89c8\u5668\u5728 window \u5bf9\u8c61\u4e0a\u8fd8\u63d0\u4f9b\u4e86\u8bb8\u591a\u5176\u4ed6\u7684\u5168\u5c40\u65b9\u6cd5\u3002<\/p>\n<blockquote>\n<p>\u58f0\u660e\u53d8\u91cf\u7684\u65b9\u6cd5\u6709\u4e09\u79cd\uff1a<strong>const<\/strong> \u3001<strong>let<\/strong>\u3001<strong>var<\/strong>\uff0c\u5b83\u4eec\u90fd\u53ef\u4ee5\u58f0\u660e\u5168\u5c40\u53d8\u91cf\uff0c\u4f46\u53ea\u6709 <strong>var<\/strong> \u58f0\u660e\u53d8\u91cf\u6302\u5728\u5230\u4e86 <strong>window<\/strong> \u4e0a\u3002<\/p>\n<\/blockquote>\n<h3><a id=\"function-or-local-scope\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>Function or Local Scope<\/h3>\n<p>\u5f53\u5728\u4e00\u4e2a\u51fd\u6570\u4e2d\u58f0\u660e\u4e00\u4e2a\u53d8\u91cf\u65f6\uff0c\u5b83\u53ea\u80fd\u5728\u51fd\u6570\u4e2d\u4f7f\u7528\u5916\u9762\u65e0\u6cd5\u4f7f\u7528\u3002\u56e0\u4e3a\u5b83\u7684\u4f5c\u7528\u57df\u53ea\u5f52\u5c5e\u4e8e\u51fd\u6570\u3002<\/p>\n<pre><code class=\"language-plain_text\">const first = &quot;Parwinder&quot;\n\nfunction sayLastName() {\n    const last = &quot;Bhagat&quot;;\n}\n\nconsole.log(first); \/\/ Parwinder\nconsole.log(last); \/\/ Reference Error: last is not defined\n<\/code><\/pre>\n<p>\u6b63\u5982\u4f60\u6240\u89c1\uff0clast \u5728\u51fd\u6570\u5916\u9762\u662f\u65e0\u6cd5\u4f7f\u7528\u7684\uff0c\u5728 sayLastName \u4e2d\u662f\u53ef\u4ee5\u4f7f\u7528\u7684\u3002<\/p>\n<pre><code class=\"language-plain_text\">const first = &quot;Parwinder&quot;\n\nfunction sayLastName() {\n    const last = &quot;Bhagat&quot;;\n    console.log(last); \/\/ Bhagat\n}\n\nsayLastName(); \/\/ Remember, I have to call a function to execute the code block in it.\n\nconsole.log(first); \/\/ Parwinder\n<\/code><\/pre>\n<p>\u6211\u4eec\u53ef\u4ee5\u8bfb\u53d6\u5168\u5c40\u53d8\u91cf\u5e76\u5728\u4efb\u4f55\u5730\u65b9\u4f7f\u7528\uff0c\u540c\u6837\u4f7f\u7528\u4e0a\u9762\u7684\u4f8b\u5b50\uff1a<\/p>\n<pre><code class=\"language-plain_text\">const first = &quot;Parwinder&quot;\n\nfunction sayLastName() {\n    console.log(first); \/\/ Parwinder\n    const last = &quot;Bhagat&quot;;\n    console.log(last); \/\/ Bhagat\n}\n\nsayLastName();\n\nconsole.log(first); \/\/ Parwinder\n<\/code><\/pre>\n<p>\u5373\u4f7f\u6211\u4e0d\u80fd\u5728\u51fd\u6570\u5916\u4e2d\u83b7\u53d6 last \uff0c\u4f46\u4f9d\u65e7\u53ef\u4ee5\u5728\u51fd\u6570\u4e2d\u83b7\u53d6\u5168\u5c40\u53d8\u91cf first \u3002\u8fd9\u662f\u56e0\u4e3a\u5728 JavaScript \u4e2d\u5f53\u89e3\u91ca\u5668\u8bfb\u53d6\u4e00\u4e2a\u53d8\u91cf\u65f6\uff0c\u9996\u5148\u5728\u5f53\u524d\u4f5c\u7528\u57df\u4e2d\u67e5\u627e\uff0c\u82e5\u6ca1\u6709\u5219\u4f1a\u5411\u4e0a\u5c42\u4f5c\u7528\u57df\u67e5\u627e\u3002\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u5f53\u6211\u4eec\u5728 sayLastName \u4e2d\u6253\u5370 first \u65f6\uff0cJS \u5f15\u64ce\u9996\u5148\u4f1a\u5728\u51fd\u6570\u4f5c\u7528\u57df\u4e2d\u67e5\u627e\uff0c\u4e4b\u540e\u5728\u5168\u5c40\u4f5c\u7528\u57df\u4e2d\u67e5\u627e\u3002\u82e5\u4efb\u4f55\u4f5c\u7528\u57df\u4e2d\u90fd\u6ca1\u6709\u627e\u5230\uff0c\u5219\u4f1a\u62a5\u4e00\u4e2a\u5f15\u7528\u7684\u9519\u8bef\u3002<\/p>\n<h3><a id=\"block-scope\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>Block Scope<\/h3>\n<p>\u5757\u7ea7\u4f5c\u7528\u57df\u662f\u88ab\u5b9a\u4e49\u5728\u4e00\u5bf9\u5927\u62ec\u53f7\u4e2d\u7684\u3002\u6839\u636e\u5b9a\u4e49\u51fd\u6570\u662f\u4e00\u4e2a\u5feb\uff0c\u4f46\u51fd\u6570\u4e2d\u8fd8\u6709\u66f4\u5c0f\u7684\u5feb\u3002\u51fd\u6570\u4e2d\u7684 <strong>for loop<\/strong> \u6216 <strong>if<\/strong> \u8bed\u53e5\u90fd\u6709\u5b83\u4eec\u81ea\u5df1\u7684\u5757\u7ea7\u4f5c\u7528\u57df\u3002\u5757\u7ea7\u4f5c\u7528\u57df\u5e2e\u52a9\u6211\u4eec\u5728\u4ee3\u7801\u4e2d\u7ec4\u7ec7\u4e86\u5f88\u5c0f\u7684\u4f5c\u7528\u57df\u3002<\/p>\n<p>\u521b\u5efa\u5757\u7ea7\u4f5c\u7528\u57df\u65b9\u6cd5\u662f\u4f7f\u7528 const \u6216 let \u5728\u5927\u62ec\u53f7\u4e2d\u58f0\u660e\u53d8\u91cf\u3002<\/p>\n<pre><code class=\"language-plain_text\">function amICool(name) {\n    let attribute;\n    if (name === &quot;Parwinder&quot;) {\n        attribute = &quot;cool&quot;\n    }\n    return attribute;\n}\n\nconsole.log(amICool(&quot;Parwinder&quot;)); \/\/ cool\nconsole.log(amICool(&quot;Bhagat&quot;)); \/\/ undefined because the name was not &quot;Parwinder&quot;\n<\/code><\/pre>\n<p>\u4e0a\u9762\u7684\u4f8b\u5b50\u662f\u4e3a attribute \u521b\u5efa\u4e86\u51fd\u6570\u4f5c\u7528\u57df\uff0c\u6211\u4eec\u53ef\u4ee5\u628a\u5b83\u7f29\u5c0f\u4e9b\uff1a<\/p>\n<pre><code class=\"language-plain_text\">function amICool(name) {\n    if (name === &quot;Parwinder&quot;) {\n       let attribute = &quot;cool&quot;\n    }\n    return attribute; \/\/ Reference error: attribute is not defined\n}\n\nconsole.log(amICool(&quot;Parwinder&quot;)); \/\/ Reference error: attribute is not defined\nconsole.log(amICool(&quot;Bhagat&quot;)); \/\/ This will not run as we ran into an error with the first execution of the function\n<\/code><\/pre>\n<p>\u53d1\u751f\u4e86\u4ec0\u4e48\uff1flet \u4f7f attribute \u53d8\u91cf\u5c5e\u4e8e\u5757\u7ea7\u4f5c\u7528\u57df\uff0c\u6b64\u65f6\u5b83\u88ab\u9650\u5236\u5728 if \u8bed\u53e5\u4e2d\u3002\u5f53\u6211\u4eec\u5c1d\u8bd5\u5728 if \u8bed\u53e5\u5916\u8fd4\u56de\u5b83\u65f6\uff0cattribute \u662f\u627e\u4e0d\u5230\u7684\u3002<\/p>\n<blockquote>\n<p>\u4e00\u4e2a\u7ecf\u9a8c\uff1a\u9996\u5148\u4f7f\u7528 const \u6765\u58f0\u660e\u53d8\u91cf\uff0c\u82e5\u53d8\u91cf\u7684\u503c\u4f1a\u53d8\u52a8\uff0c\u5219\u4f7f\u7528 let \u6b21\u4e4b\uff0cvar \u662f\u6700\u540e\u7684\u9009\u62e9\u3002<\/p>\n<\/blockquote>\n<h3><a id=\"lexical-scoping\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>Lexical Scoping<\/h3>\n<p>\u7b49\u7b49\uff0c\u8fd9\u662f\u4ec0\u4e48\uff1f\u4e0d\u662f\u8bf4\u53ea\u6709\u4e09\u79cd\u4f5c\u7528\u57df\u5417\uff1f\u8bcd\u6cd5\u4f5c\u7528\u57df\u662f JavaScript \u4e2d\u4f7f\u7528\u7684\u4f5c\u7528\u57df\u6a21\u578b\u3002\u53d8\u91cf\u6216\u51fd\u6570\u7684\u4f5c\u7528\u57df\u662f\u5b83\u4eec\u88ab\u5b9a\u4e49\u65f6\u6240\u5728\u7684\u4f5c\u7528\u57df\u3002<\/p>\n<ul>\n<li>\u8bcd\u6cd5\u4f5c\u7528\u57df\u53c8\u53eb\u9759\u6001\u4f5c\u7528\u57df\u3002<\/li>\n<li>\u4e00\u4e2a\u58f0\u660e \u88ab\u8c03\u7528\u65f6\u7684\u4f5c\u7528\u57df \u4e0d\u4e00\u5b9a\u662f\u5b83\u7684\u8bcd\u6cd5\u4f5c\u7528\u57df\u3002\u76f8\u53cd\u7684\uff0c\u5b9a\u4e49\u65f6\u7684\u4f5c\u7528\u57df \u624d\u662f\u8bcd\u6cd5\u4f5c\u7528\u57df<\/li>\n<\/ul>\n<pre><code class=\"language-plain_text\">const myName = &quot;Parwinder&quot;;\n\nfunction sayMyName() {\n    console.log(myName);\n}\n\nfunction sayMyNameAgain() {\n    const myName = &quot;Bhagat&quot;;\n    sayMyName();\n}\n\nsayMyNameAgain();\n<\/code><\/pre>\n<p>\u8f93\u51fa <em>Parwinder<\/em> \uff0c\u4e3a\u4ec0\u4e48\uff1a<\/p>\n<ol>\n<li>sayMyNameAgain \u6267\u884c\u65f6\uff0c\u5185\u90e8\u8c03\u7528\u4e86 sayMyName\u3002<\/li>\n<li>\u5373\u4f7f sayMyNameAgain \u5185\u90e8\u6709\u4e00\u4e2a myName \u7684\u53d8\u91cf\uff0c\u4f46\u5b83\u4e0d\u4f1a\u88ab\u6253\u5370\u3002<\/li>\n<li>sayMyNameAgain \u6253\u5370\u5168\u5c40\u53d8\u91cf myName \u7684\u503c\uff1aParwinder\u3002<\/li>\n<\/ol>\n<p>\u56e0\u4e3a sayMyName \u662f\u5728\u58f0\u660e\u5b83\u7684\u4f5c\u7528\u57df\u4e0b\u6267\u884c\u7684\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u539f\u6587\u5730\u5740\uff1ahttps:\/\/dev.to\/bhagatparwinder\/scope-in-javas&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[249,2,20,3],"tags":[107,6,106,105,108],"class_list":["post-1112","post","type-post","status-publish","format-standard","hentry","category-javascript","category-all","category-frontend","category-tech","tag-const","tag-javascript","tag-let","tag-scope","tag-var"],"_links":{"self":[{"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/posts\/1112","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1112"}],"version-history":[{"count":1,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/posts\/1112\/revisions"}],"predecessor-version":[{"id":1113,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/posts\/1112\/revisions\/1113"}],"wp:attachment":[{"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1112"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1112"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1112"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}