{"id":180,"date":"2026-01-21T10:12:00","date_gmt":"2026-01-21T02:12:00","guid":{"rendered":"https:\/\/mitongxue.cn\/?p=180"},"modified":"2026-06-04T10:54:28","modified_gmt":"2026-06-04T02:54:28","slug":"%e4%bd%9c%e7%94%a8%e5%9f%9f%e5%92%8c%e4%bd%9c%e7%94%a8%e5%9f%9f%e9%93%be","status":"publish","type":"post","link":"https:\/\/mitongxue.cn\/index.php\/2026\/01\/21\/%e4%bd%9c%e7%94%a8%e5%9f%9f%e5%92%8c%e4%bd%9c%e7%94%a8%e5%9f%9f%e9%93%be\/","title":{"rendered":"\u4f5c\u7528\u57df\u548c\u4f5c\u7528\u57df\u94fe"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">\u4ec0\u4e48\u662f\u4f5c\u7528\u57df<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f5c\u7528\u57df\u662f\u5728\u8fd0\u884c\u65f6\u4ee3\u7801\u4e2d\u7684\u67d0\u4e9b\u7279\u5b9a\u90e8\u5206\u4e2d\u53d8\u91cf\uff0c\u51fd\u6570\u548c\u5bf9\u8c61\u7684\u53ef\u8bbf\u95ee\u6027\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u6362\u53e5\u8bdd\u8bf4\uff0c\u4f5c\u7528\u57df\u51b3\u5b9a\u4e86\u4ee3\u7801\u533a\u5757\u4e2d\u53d8\u91cf\u548c\u5176\u4ed6\u8d44\u6e90\u7684\u53ef\u89c1\u6027\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>function outFun2() {\n    var inVariable = \"\u5185\u5c42\u53d8\u91cf2\";\n}\noutFun2();\nconsole.log(inVariable); \/\/ Uncaught ReferenceError: inVariable is not defined<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u4ece\u4e0a\u9762\u7684\u4f8b\u5b50\u53ef\u4ee5\u4f53\u4f1a\u5230\u4f5c\u7528\u57df\u7684\u6982\u5ff5\uff0c\u53d8\u91cf <em>inVariable<\/em> \u5728\u5168\u5c40\u4f5c\u7528\u57df\u6ca1\u6709\u58f0\u660e\uff0c\u6240\u4ee5\u5728\u5168\u5c40\u4f5c\u7528\u57df\u4e0b\u53d6\u503c\u4f1a\u62a5\u9519\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u6211\u4eec\u53ef\u4ee5\u8fd9\u6837\u7406\u89e3\uff1a<strong>\u4f5c\u7528\u57df\u5c31\u662f\u4e00\u4e2a\u72ec\u7acb\u7684\u5730\u76d8\uff0c\u8ba9\u53d8\u91cf\u4e0d\u4f1a\u5916\u6cc4\u3001\u66b4\u9732\u51fa\u53bb<\/strong>\u3002\u4e5f\u5c31\u662f\u8bf4<strong>\u4f5c\u7528\u57df\u6700\u5927\u7684\u7528\u5904\u5c31\u662f\u9694\u79bb\u53d8\u91cf\uff0c\u4e0d\u540c\u4f5c\u7528\u57df\u4e0b\u540c\u540d\u53d8\u91cf\u4e0d\u4f1a\u6709\u51b2\u7a81\u3002<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong><em>ES6<\/em> \u4e4b\u524d <em>JavaScript<\/em> \u6ca1\u6709\u5757\u7ea7\u4f5c\u7528\u57df\uff0c\u53ea\u6709\u5168\u5c40\u4f5c\u7528\u57df\u548c\u51fd\u6570\u4f5c\u7528\u57df<\/strong>\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><em>ES6<\/em> \u7684\u5230\u6765\uff0c\u4e3a\u6211\u4eec\u63d0\u4f9b\u4e86\u201c\u5757\u7ea7\u4f5c\u7528\u57df\u201d\uff0c\u53ef\u901a\u8fc7\u65b0\u589e\u547d\u4ee4 <em>let<\/em> \u548c <em>const<\/em> \u6765\u4f53\u73b0\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u5168\u5c40\u4f5c\u7528\u57df\u548c\u51fd\u6570\u4f5c\u7528\u57df<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\uff081\uff09\u5168\u5c40\u4f5c\u7528\u57df<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u4ee3\u7801\u4e2d\u4efb\u4f55\u5730\u65b9\u90fd\u80fd\u8bbf\u95ee\u5230\u7684\u5bf9\u8c61\u62e5\u6709\u5168\u5c40\u4f5c\u7528\u57df\uff0c\u4e00\u822c\u6765\u8bf4\u4ee5\u4e0b\u51e0\u79cd\u60c5\u5f62\u62e5\u6709\u5168\u5c40\u4f5c\u7528\u57df\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u6700\u5916\u5c42\u51fd\u6570\u548c\u5728\u6700\u5916\u5c42\u51fd\u6570\u5916\u9762\u5b9a\u4e49\u7684\u53d8\u91cf\u62e5\u6709\u5168\u5c40\u4f5c\u7528\u57df<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>var outVariable = \"\u6211\u662f\u6700\u5916\u5c42\u53d8\u91cf\"; \/\/\u6700\u5916\u5c42\u53d8\u91cf\nfunction outFun() { \/\/\u6700\u5916\u5c42\u51fd\u6570\n    var inVariable = \"\u5185\u5c42\u53d8\u91cf\";\n    function innerFun() { \/\/\u5185\u5c42\u51fd\u6570\n        console.log(inVariable);\n    }\n    innerFun();\n}\nconsole.log(outVariable); \/\/ \u6211\u662f\u6700\u5916\u5c42\u53d8\u91cf\noutFun(); \/\/ \u5185\u5c42\u53d8\u91cf\nconsole.log(inVariable); \/\/ inVariable is not defined\ninnerFun(); \/\/ innerFun is not defined<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u6240\u6709\u672a\u5b9a\u4e49\u76f4\u63a5\u8d4b\u503c\u7684\u53d8\u91cf\u81ea\u52a8\u58f0\u660e\u4e3a\u62e5\u6709\u5168\u5c40\u4f5c\u7528\u57df<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>function outFun2() {\n    variable = \"\u672a\u5b9a\u4e49\u76f4\u63a5\u8d4b\u503c\u7684\u53d8\u91cf\";\n    var inVariable2 = \"\u5185\u5c42\u53d8\u91cf2\";\n}\noutFun2();\/\/\u8981\u5148\u6267\u884c\u8fd9\u4e2a\u51fd\u6570\uff0c\u5426\u5219\u6839\u672c\u4e0d\u77e5\u9053\u91cc\u9762\u662f\u5565\nconsole.log(variable); \/\/\u672a\u5b9a\u4e49\u76f4\u63a5\u8d4b\u503c\u7684\u53d8\u91cf\nconsole.log(inVariable2); \/\/inVariable2 is not defined<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u6240\u6709 <em>window<\/em> \u5bf9\u8c61\u7684\u5c5e\u6027\u62e5\u6709\u5168\u5c40\u4f5c\u7528\u57df<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e00\u822c\u60c5\u51b5\u4e0b\uff0c<em>window<\/em> \u5bf9\u8c61\u7684\u5185\u7f6e\u5c5e\u6027\u90fd\u62e5\u6709\u5168\u5c40\u4f5c\u7528\u57df\uff0c\u4f8b\u5982 <em>window.name\u3001window.location\u3001window.top<\/em> \u7b49\u7b49\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5168\u5c40\u4f5c\u7528\u57df\u6709\u4e2a\u5f0a\u7aef\uff1a\u5982\u679c\u6211\u4eec\u5199\u4e86\u5f88\u591a\u884c <em>JS<\/em> \u4ee3\u7801\uff0c\u53d8\u91cf\u5b9a\u4e49\u90fd\u6ca1\u6709\u7528\u51fd\u6570\u5305\u62ec\uff0c\u90a3\u4e48\u5b83\u4eec\u5c31\u5168\u90e8\u90fd\u5728\u5168\u5c40\u4f5c\u7528\u57df\u4e2d\u3002\u8fd9\u6837\u5c31\u4f1a\u6c61\u67d3\u5168\u5c40\u547d\u540d\u7a7a\u95f4\uff0c \u5bb9\u6613\u5f15\u8d77\u547d\u540d\u51b2\u7a81\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ \u5f20\u4e09\u5199\u7684\u4ee3\u7801\u4e2d\nvar data = {a: 100}\n\n\/\/ \u674e\u56db\u5199\u7684\u4ee3\u7801\u4e2d\nvar data = {x: true}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u5c31\u662f\u4e3a\u4f55 <em>jQuery\u3001Zepto<\/em> \u7b49\u5e93\u7684\u6e90\u7801\uff0c\u6240\u6709\u7684\u4ee3\u7801\u90fd\u4f1a\u653e\u5728 <em>(function(){&#8230;.})( )<\/em> \u4e2d\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u56e0\u4e3a\u653e\u5728\u91cc\u9762\u7684\u6240\u6709\u53d8\u91cf\uff0c\u90fd\u4e0d\u4f1a\u88ab\u5916\u6cc4\u548c\u66b4\u9732\uff0c\u4e0d\u4f1a\u6c61\u67d3\u5230\u5916\u9762\uff0c\u4e0d\u4f1a\u5bf9\u5176\u4ed6\u7684\u5e93\u6216\u8005 <em>JS<\/em> \u811a\u672c\u9020\u6210\u5f71\u54cd\u3002\u8fd9\u662f\u51fd\u6570\u4f5c\u7528\u57df\u7684\u4e00\u4e2a\u4f53\u73b0\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\uff082\uff09\u51fd\u6570\u4f5c\u7528\u57df<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u51fd\u6570\u4f5c\u7528\u57df\uff0c\u662f\u6307\u58f0\u660e\u5728\u51fd\u6570\u5185\u90e8\u7684\u53d8\u91cf\uff0c\u548c\u5168\u5c40\u4f5c\u7528\u57df\u76f8\u53cd\uff0c\u5c40\u90e8\u4f5c\u7528\u57df\u4e00\u822c\u53ea\u5728\u56fa\u5b9a\u7684\u4ee3\u7801\u7247\u6bb5\u5185\u53ef\u8bbf\u95ee\u5230\uff0c\u6700\u5e38\u89c1\u7684\u4f8b\u5982\u51fd\u6570\u5185\u90e8\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>function doSomething(){\n    var stuName=\"zhangsan\";\n    function innerSay(){\n        console.log(stuName);\n    }\n    innerSay();\n}\nconsole.log(stuName); \/\/ \u811a\u672c\u9519\u8bef\ninnerSay(); \/\/ \u811a\u672c\u9519\u8bef<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u4f5c\u7528\u57df\u662f\u5206\u5c42\u7684\uff0c\u5185\u5c42\u4f5c\u7528\u57df\u53ef\u4ee5\u8bbf\u95ee\u5916\u5c42\u4f5c\u7528\u57df\u7684\u53d8\u91cf\uff0c\u53cd\u4e4b\u5219\u4e0d\u884c<\/strong>\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u503c\u5f97\u6ce8\u610f\u7684\u662f\uff1a<strong>\u5757\u8bed\u53e5\uff08\u5927\u62ec\u53f7\u201c\uff5b \uff5d\u201d\u4e2d\u95f4\u7684\u8bed\u53e5\uff09\uff0c\u5982 <em>if<\/em> \u548c <em>switch<\/em> \u6761\u4ef6\u8bed\u53e5\u6216 <em>for<\/em> \u548c <em>while<\/em> \u5faa\u73af\u8bed\u53e5\uff0c\u4e0d\u50cf\u51fd\u6570\uff0c\u5b83\u4eec\u4e0d\u4f1a\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u4f5c\u7528\u57df<\/strong>\u3002\u5728\u5757\u8bed\u53e5\u4e2d\u5b9a\u4e49\u7684\u53d8\u91cf\u5c06\u4fdd\u7559\u5728\u5b83\u4eec\u5df2\u7ecf\u5b58\u5728\u7684\u4f5c\u7528\u57df\u4e2d\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>if (true) {\n    \/\/ 'if' \u6761\u4ef6\u8bed\u53e5\u5757\u4e0d\u4f1a\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u4f5c\u7528\u57df\n    var name = 'Hammad'; \/\/ name \u4f9d\u7136\u5728\u5168\u5c40\u4f5c\u7528\u57df\u4e2d\n}\nconsole.log(name); \/\/ logs 'Hammad'<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u5757\u7ea7\u4f5c\u7528\u57df<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u5757\u7ea7\u4f5c\u7528\u57df\u53ef\u901a\u8fc7\u65b0\u589e\u547d\u4ee4 <em>let<\/em> \u548c <em>const<\/em> \u58f0\u660e\uff0c\u6240\u58f0\u660e\u7684\u53d8\u91cf\u5728\u6307\u5b9a\u5757\u7684\u4f5c\u7528\u57df\u5916\u65e0\u6cd5\u88ab\u8bbf\u95ee\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5757\u7ea7\u4f5c\u7528\u57df\u5728\u5982\u4e0b\u60c5\u51b5\u88ab\u521b\u5efa\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u5728\u4e00\u4e2a\u51fd\u6570\u5185\u90e8<\/li>\n\n\n\n<li>\u5728\u4e00\u4e2a\u4ee3\u7801\u5757\uff08\u7531\u4e00\u5bf9\u82b1\u62ec\u53f7\u5305\u88f9\uff09\u5185\u90e8<\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\"><em>let<\/em> \u58f0\u660e\u7684\u8bed\u6cd5\u4e0e <em>var<\/em> \u7684\u8bed\u6cd5\u4e00\u81f4\u3002\u4f60\u57fa\u672c\u4e0a\u53ef\u4ee5\u7528 <em>let<\/em> \u6765\u4ee3\u66ff <em>var<\/em> \u8fdb\u884c\u53d8\u91cf\u58f0\u660e\uff0c\u4f46\u4f1a\u5c06\u53d8\u91cf\u7684\u4f5c\u7528\u57df\u9650\u5236\u5728\u5f53\u524d\u4ee3\u7801\u5757\u4e2d\u3002\u5757\u7ea7\u4f5c\u7528\u57df\u6709\u4ee5\u4e0b\u51e0\u4e2a\u7279\u70b9\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u58f0\u660e\u53d8\u91cf\u4e0d\u4f1a\u63d0\u5347\u5230\u4ee3\u7801\u5757\u9876\u90e8<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><em>let\u3001const<\/em> \u58f0\u660e\u5e76\u4e0d\u4f1a\u88ab\u63d0\u5347\u5230\u5f53\u524d\u4ee3\u7801\u5757\u7684\u9876\u90e8\uff0c\u56e0\u6b64\u4f60\u9700\u8981\u624b\u52a8\u5c06 <em>let\u3001const<\/em> \u58f0\u660e\u653e\u7f6e\u5230\u9876\u90e8\uff0c\u4ee5\u4fbf\u8ba9\u53d8\u91cf\u5728\u6574\u4e2a\u4ee3\u7801\u5757\u5185\u90e8\u53ef\u7528\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>function getValue(condition) {\n    if (condition) {\n        let value = \"blue\";\n        return value;\n    } else {\n        \/\/ value \u5728\u6b64\u5904\u4e0d\u53ef\u7528\n        return null;\n    }\n    \/\/ value \u5728\u6b64\u5904\u4e0d\u53ef\u7528\n}<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u7981\u6b62\u91cd\u590d\u58f0\u660e<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679c\u4e00\u4e2a\u6807\u8bc6\u7b26\u5df2\u7ecf\u5728\u4ee3\u7801\u5757\u5185\u90e8\u88ab\u5b9a\u4e49\uff0c\u90a3\u4e48\u5728\u6b64\u4ee3\u7801\u5757\u5185\u4f7f\u7528\u540c\u4e00\u4e2a\u6807\u8bc6\u7b26\u8fdb\u884c <em>let<\/em> \u58f0\u660e\u5c31\u4f1a\u5bfc\u81f4\u629b\u51fa\u9519\u8bef\u3002\u4f8b\u5982\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>var count = 30;\nlet count = 40; \/\/ Uncaught SyntaxError: Identifier 'count' has already been declared<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u672c\u4f8b\u4e2d\uff0c <em>count<\/em> \u53d8\u91cf\u88ab\u58f0\u660e\u4e86\u4e24\u6b21\uff1a\u4e00\u6b21\u4f7f\u7528 <em>var<\/em> \uff0c\u53e6\u4e00\u6b21\u4f7f\u7528 <em>let<\/em>\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u56e0\u4e3a <em>let<\/em> \u4e0d\u80fd\u5728\u540c\u4e00\u4f5c\u7528\u57df\u5185\u91cd\u590d\u58f0\u660e\u4e00\u4e2a\u5df2\u6709\u6807\u8bc6\u7b26\uff0c\u6b64\u5904\u7684 <em>let<\/em> \u58f0\u660e\u5c31\u4f1a\u629b\u51fa\u9519\u8bef\u3002\u4f46\u5982\u679c\u5728\u5d4c\u5957\u7684\u4f5c\u7528\u57df\u5185\u4f7f\u7528 <em>let<\/em> \u58f0\u660e\u4e00\u4e2a\u540c\u540d\u7684\u65b0\u53d8\u91cf\uff0c\u5219\u4e0d\u4f1a\u629b\u51fa\u9519\u8bef\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>var count = 30;\n\/\/ \u4e0d\u4f1a\u629b\u51fa\u9519\u8bef\nif (condition) {\n    let count = 40;\n    \/\/ \u5176\u4ed6\u4ee3\u7801\n}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u4f5c\u7528\u57df\u94fe<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">\u4ec0\u4e48\u662f\u81ea\u7531\u53d8\u91cf<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u9996\u5148\u8ba4\u8bc6\u4e00\u4e0b\u4ec0\u4e48\u53eb\u505a<strong>\u81ea\u7531\u53d8\u91cf<\/strong> \u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u4e0b\u4ee3\u7801\u4e2d\uff0c<em>console.log(a)<\/em> \u8981\u5f97\u5230 <em>a<\/em> \u53d8\u91cf\uff0c\u4f46\u662f\u5728\u5f53\u524d\u7684\u4f5c\u7528\u57df\u4e2d\u6ca1\u6709\u5b9a\u4e49 <em>a<\/em>\uff08\u53ef\u5bf9\u6bd4\u4e00\u4e0b <em>b<\/em>\uff09\u3002\u5f53\u524d\u4f5c\u7528\u57df\u6ca1\u6709\u5b9a\u4e49\u7684\u53d8\u91cf\uff0c\u8fd9\u6210\u4e3a\u81ea\u7531\u53d8\u91cf \u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u81ea\u7531\u53d8\u91cf\u7684\u503c\u5982\u4f55\u5f97\u5230 \uff1f<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u9700\u8981\u5411\u7236\u7ea7\u4f5c\u7528\u57df\u5bfb\u627e\uff08\u6ce8\u610f\uff1a\u8fd9\u79cd\u8bf4\u6cd5\u5e76\u4e0d\u4e25\u8c28\uff0c\u4e0b\u6587\u4f1a\u91cd\u70b9\u89e3\u91ca\uff09\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>var a = 100\nfunction fn() {\n    var b = 200\n    console.log(a) \/\/ \u8fd9\u91cc\u7684 a \u5728\u8fd9\u91cc\u5c31\u662f\u4e00\u4e2a\u81ea\u7531\u53d8\u91cf\n    console.log(b)\n}\nfn()<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u4ec0\u4e48\u662f\u4f5c\u7528\u57df\u94fe<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679c\u7236\u7ea7\u4e5f\u6ca1\u5462\uff1f<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u518d\u4e00\u5c42\u4e00\u5c42\u5411\u4e0a\u5bfb\u627e\uff0c\u76f4\u5230\u627e\u5230\u5168\u5c40\u4f5c\u7528\u57df\u8fd8\u662f\u6ca1\u627e\u5230\uff0c\u5c31\u5ba3\u5e03\u653e\u5f03\u3002\u8fd9\u79cd\u4e00\u5c42\u4e00\u5c42\u7684\u5173\u7cfb\uff0c\u5c31\u662f\u4f5c\u7528\u57df\u94fe \u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>var a = 100\nfunction f1() {\n    var b = 200\n    function f2() {\n        var c = 300\n        console.log(a) \/\/ 100 \u81ea\u7531\u53d8\u91cf\uff0c\u987a\u4f5c\u7528\u57df\u94fe\u5411\u7236\u4f5c\u7528\u57df\u627e\n        console.log(b) \/\/ 200 \u81ea\u7531\u53d8\u91cf\uff0c\u987a\u4f5c\u7528\u57df\u94fe\u5411\u7236\u4f5c\u7528\u57df\u627e\n        console.log(c) \/\/ 300 \u672c\u4f5c\u7528\u57df\u7684\u53d8\u91cf\n    }\n    f2()\n}\nf1()<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u5173\u4e8e\u81ea\u7531\u53d8\u91cf\u7684\u53d6\u503c<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5173\u4e8e\u81ea\u7531\u53d8\u91cf\u7684\u503c\uff0c\u4e0a\u6587\u63d0\u5230\u8981\u5230\u7236\u4f5c\u7528\u57df\u4e2d\u53d6\uff0c\u5176\u5b9e\u6709\u65f6\u5019\u8fd9\u79cd\u89e3\u91ca\u4f1a\u4ea7\u751f\u6b67\u4e49\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>var x = 10\nfunction fn() {\n    console.log(x)\n}\nfunction show(f) {\n    var x = 20;\n    (function () {\n        f() \/\/ 10\uff0c\u800c\u4e0d\u662f 20\n    })()\n}\nshow(fn)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728 <em>fn<\/em> \u51fd\u6570\u4e2d\uff0c\u53d6\u81ea\u7531\u53d8\u91cf <em>x<\/em> \u7684\u503c\u65f6\uff0c\u8981\u5230\u54ea\u4e2a\u4f5c\u7528\u57df\u4e2d\u53d6 \uff1f<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u8981\u5230\u521b\u5efa <em>fn<\/em> \u51fd\u6570\u7684\u90a3\u4e2a\u4f5c\u7528\u57df\u4e2d\u53d6\uff0c<strong>\u65e0\u8bba <em>fn<\/em> \u51fd\u6570\u5c06\u5728\u54ea\u91cc\u8c03\u7528<\/strong>\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u6240\u4ee5\uff0c\u4e0d\u8981\u5728\u7528\u4ee5\u4e0a\u8bf4\u6cd5\u4e86\u3002\u76f8\u6bd4\u800c\u8a00\uff0c\u7528\u8fd9\u53e5\u8bdd\u63cf\u8ff0\u4f1a\u66f4\u52a0\u8d34\u5207\uff1a<strong>\u8981\u5230\u521b\u5efa\u8fd9\u4e2a\u51fd\u6570\u7684\u90a3\u4e2a\u57df\u201d\u3002\u4f5c\u7528\u57df\u4e2d\u53d6\u503c\uff0c\u8fd9\u91cc\u5f3a\u8c03\u7684\u662f\u201c\u521b\u5efa\u201d\uff0c\u800c\u4e0d\u662f\u201c\u8c03\u7528\u201d<\/strong>\uff0c\u5207\u8bb0\u5207\u8bb0\uff0c\u5176\u5b9e\u8fd9\u5c31\u662f\u6240\u8c13\u7684&#8221;\u9759\u6001\u4f5c\u7528\u57df&#8221;\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u518d\u6765\u770b\u4e00\u4e2a\u4f8b\u5b50\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const food = \"rice\";\nconst eat = function () {\n    console.log(`eat ${food}`);\n};\n(function () {\n    const food = \"noodle\";\n    eat(); \/\/ eat rice\n})();<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u672c\u793a\u4f8b\u4e2d\uff0c\u6700\u7ec8\u6253\u5370\u7684\u7ed3\u679c\u4e3a <em>eat rice<\/em>\u3002\u56e0\u4e3a\u5bf9\u4e8e <em>eat( )<\/em> \u51fd\u6570\u6765\u8bf4\uff0c\u521b\u5efa\u8be5\u51fd\u6570\u65f6\u5b83\u7684\u7236\u7ea7\u4e0a\u4e0b\u6587\u4e3a\u5168\u5c40\u4e0a\u4e0b\u6587\uff0c\u6240\u4ee5 <em>food<\/em> \u7684\u503c\u4e3a <em>rice<\/em>\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679c\u6211\u4eec\u5c06\u4ee3\u7801\u7a0d\u4f5c\u4fee\u6539\uff0c\u6539\u6210\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const food = \"rice\";\n(function () {\n    const food = \"noodle\";\n    const eat = function () {\n        console.log(`eat ${food}`);\n    };\n    eat(); \/\/ eat noodle\n})();<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u4e2a\u65f6\u5019\uff0c\u6253\u5370\u51fa\u6765\u7684\u503c\u5c31\u4e3a <em>eat noodle<\/em>\u3002\u56e0\u4e3a\u5bf9\u4e8e <em>eat( )<\/em> \u51fd\u6570\u6765\u8bb2\uff0c\u521b\u5efa\u5b83\u7684\u65f6\u5019\u7236\u7ea7\u4e0a\u4e0b\u6587\u4e3a <em>IIFE<\/em>\uff0c\u6240\u4ee5 <em>food<\/em> \u7684\u503c\u4e3a <em>noodle<\/em>\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u4f5c\u7528\u57df\u4e0e\u6267\u884c\u4e0a\u4e0b\u6587<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u8bb8\u591a\u5f00\u53d1\u4eba\u5458\u7ecf\u5e38\u6df7\u6dc6\u4f5c\u7528\u57df\u548c\u6267\u884c\u4e0a\u4e0b\u6587\u7684\u6982\u5ff5\uff0c\u8bef\u8ba4\u4e3a\u5b83\u4eec\u662f\u76f8\u540c\u7684\u6982\u5ff5\uff0c\u4f46\u4e8b\u5b9e\u5e76\u975e\u5982\u6b64\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u6211\u4eec\u77e5\u9053 <em>JavaScript<\/em> \u5c5e\u4e8e\u89e3\u91ca\u578b\u8bed\u8a00\uff0c<em>JavaScript<\/em> \u7684\u6267\u884c\u5206\u4e3a\uff1a\u89e3\u91ca\u548c\u6267\u884c\u4e24\u4e2a\u9636\u6bb5\uff0c\u8fd9\u4e24\u4e2a\u9636\u6bb5\u6240\u505a\u7684\u4e8b\u5e76\u4e0d\u4e00\u6837\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u89e3\u91ca\u9636\u6bb5<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u8bcd\u6cd5\u5206\u6790<\/li>\n\n\n\n<li>\u8bed\u6cd5\u5206\u6790<\/li>\n\n\n\n<li>\u4f5c\u7528\u57df\u89c4\u5219\u786e\u5b9a<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u6267\u884c\u9636\u6bb5<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u521b\u5efa\u6267\u884c\u4e0a\u4e0b\u6587<\/li>\n\n\n\n<li>\u6267\u884c\u51fd\u6570\u4ee3\u7801<\/li>\n\n\n\n<li>\u5783\u573e\u56de\u6536<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><em>JavaScript<\/em> \u89e3\u91ca\u9636\u6bb5\u4fbf\u4f1a\u786e\u5b9a\u4f5c\u7528\u57df\u89c4\u5219\uff0c\u56e0\u6b64\u4f5c\u7528\u57df\u5728\u51fd\u6570\u5b9a\u4e49\u65f6\u5c31\u5df2\u7ecf\u786e\u5b9a\u4e86\uff0c\u800c\u4e0d\u662f\u5728\u51fd\u6570\u8c03\u7528\u65f6\u786e\u5b9a\uff0c\u4f46\u662f\u6267\u884c\u4e0a\u4e0b\u6587\u662f\u51fd\u6570\u6267\u884c\u4e4b\u524d\u521b\u5efa\u7684\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u6267\u884c\u4e0a\u4e0b\u6587\u6700\u660e\u663e\u7684\u5c31\u662f <em>this<\/em> \u7684\u6307\u5411\u662f\u6267\u884c\u65f6\u786e\u5b9a\u7684\u3002\u800c\u4f5c\u7528\u57df\u8bbf\u95ee\u7684\u53d8\u91cf\u662f\u7f16\u5199\u4ee3\u7801\u7684\u7ed3\u6784\u786e\u5b9a\u7684\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f5c\u7528\u57df\u548c\u6267\u884c\u4e0a\u4e0b\u6587\u4e4b\u95f4\u6700\u5927\u7684\u533a\u522b\u662f\uff1a<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u6267\u884c\u4e0a\u4e0b\u6587\u5728\u8fd0\u884c\u65f6\u786e\u5b9a\uff0c\u968f\u65f6\u53ef\u80fd\u6539\u53d8\uff0c\u4f5c\u7528\u57df\u5728\u5b9a\u4e49\u65f6\u5c31\u786e\u5b9a\uff0c\u5e76\u4e14\u4e0d\u4f1a\u6539\u53d8<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4ec0\u4e48\u662f\u4f5c\u7528\u57df \u4f5c\u7528\u57df\u662f\u5728\u8fd0\u884c\u65f6\u4ee3\u7801\u4e2d\u7684\u67d0\u4e9b\u7279\u5b9a\u90e8\u5206..<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16,11],"tags":[],"class_list":["post-180","post","type-post","status-publish","format-standard","hentry","category-js","category-front"],"featured_image_urls":{"full":"","thumbnail":"","medium":"","medium_large":"","large":"","1536x1536":"","2048x2048":""},"author_info":{"info":["\u9648 \u67d0\u4eba"]},"category_info":"<a href=\"https:\/\/mitongxue.cn\/index.php\/category\/front\/js\/\" rel=\"category tag\">JS<\/a> <a href=\"https:\/\/mitongxue.cn\/index.php\/category\/front\/\" rel=\"category tag\">\u524d\u7aef<\/a>","tag_info":"\u524d\u7aef","comment_count":"0","_links":{"self":[{"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/posts\/180","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/comments?post=180"}],"version-history":[{"count":1,"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/posts\/180\/revisions"}],"predecessor-version":[{"id":181,"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/posts\/180\/revisions\/181"}],"wp:attachment":[{"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/media?parent=180"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/categories?post=180"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/tags?post=180"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}