{"id":120,"date":"2017-02-19T14:22:24","date_gmt":"2017-02-19T06:22:24","guid":{"rendered":"http:\/\/blog.xrhoujie.com\/?p=120"},"modified":"2022-07-12T14:46:54","modified_gmt":"2022-07-12T06:46:54","slug":"javascript%e6%b7%b1%e5%85%a5%e7%90%86%e8%a7%a3js%e9%97%ad%e5%8c%85","status":"publish","type":"post","link":"http:\/\/blog.xrhoujie.com\/?p=120","title":{"rendered":"(\u8f6c)javascript\u6df1\u5165\u7406\u89e3js\u95ed\u5305"},"content":{"rendered":"<div class=\"blog-abstract\">\u6458\u8981: \u95ed\u5305\uff08closure\uff09\u662fJavascript\u8bed\u8a00\u7684\u4e00\u4e2a\u96be\u70b9\uff0c\u4e5f\u662f\u5b83\u7684\u7279\u8272\uff0c\u5f88\u591a\u9ad8\u7ea7\u5e94\u7528\u90fd\u8981\u4f9d\u9760\u95ed\u5305\u5b9e\u73b0\u3002<\/div>\n<div><\/div>\n<div><strong>\u95ed\u5305 \u89e3\u51b3\u51fd\u6570\u4f5c\u7528\u57df\u95ee\u9898 \uff0c\u8868\u73b0\u5f62\u5f0f \u4e3a \u51fd\u6570\u5957\u51fd\u6570 \uff0c\u5185\u90e8\u53ef\u8bbf\u95ee\u5916\u90e8\uff0c\u5916\u90e8\u65e0\u6cd5\u8bbf\u95ee\u5185\u90e8\u3002\u901a\u8fc7\u95ed\u5305 \u5faa\u73af\u5f15\u7528\u53d8\u91cf\uff0c\u4f7f\u53d8\u91cf\u5e38\u9a7b\u5185\u5b58\uff0c\u63d0\u5347\u6210\u4e3a\u5168\u5c40\u53d8\u91cf\u3002<\/strong><\/div>\n<div id=\"blogBody\" class=\"blog-body\">\n<div class=\"BlogContent\">\n<p><strong>\u4e00\u3001\u53d8\u91cf\u7684\u4f5c\u7528\u57df<\/strong><\/p>\n<p>\u8981\u7406\u89e3\u95ed\u5305\uff0c\u9996\u5148\u5fc5\u987b\u7406\u89e3Javascript\u7279\u6b8a\u7684\u53d8\u91cf\u4f5c\u7528\u57df\u3002<\/p>\n<p>\u53d8\u91cf\u7684\u4f5c\u7528\u57df\u65e0\u975e\u5c31\u662f\u4e24\u79cd\uff1a\u5168\u5c40\u53d8\u91cf\u548c\u5c40\u90e8\u53d8\u91cf\u3002<\/p>\n<p>Javascript\u8bed\u8a00\u7684\u7279\u6b8a\u4e4b\u5904\uff0c\u5c31\u5728\u4e8e\u51fd\u6570\u5185\u90e8\u53ef\u4ee5\u76f4\u63a5\u8bfb\u53d6\u5168\u5c40\u53d8\u91cf\u3002<br \/>\nJs\u4ee3\u7801<\/p>\n<p>var n=999;<\/p>\n<p>function f1(){<br \/>\nalert(n);<br \/>\n}<\/p>\n<p>f1(); \/\/ 999<\/p>\n<p>\u53e6\u4e00\u65b9\u9762\uff0c\u5728\u51fd\u6570\u5916\u90e8\u81ea\u7136\u65e0\u6cd5\u8bfb\u53d6\u51fd\u6570\u5185\u7684\u5c40\u90e8\u53d8\u91cf\u3002<\/p>\n<p>Js\u4ee3\u7801<\/p>\n<p>function f1(){<br \/>\nvar n=999;<br \/>\n}<\/p>\n<p>alert(n); \/\/ error<\/p>\n<p>\u8fd9\u91cc\u6709\u4e00\u4e2a\u5730\u65b9\u9700\u8981\u6ce8\u610f\uff0c\u51fd\u6570\u5185\u90e8\u58f0\u660e\u53d8\u91cf\u7684\u65f6\u5019\uff0c\u4e00\u5b9a\u8981\u4f7f\u7528var\u547d\u4ee4\u3002\u5982\u679c\u4e0d\u7528\u7684\u8bdd\uff0c\u4f60\u5b9e\u9645\u4e0a\u58f0\u660e\u4e86\u4e00\u4e2a\u5168\u5c40\u53d8\u91cf\uff01<\/p>\n<p>Js\u4ee3\u7801<\/p>\n<p>function f1(){<br \/>\nn=999;<br \/>\n}<\/p>\n<p>f1();<\/p>\n<p>alert(n); \/\/ 999<\/p>\n<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<\/p>\n<p><strong>\u4e8c\u3001\u5982\u4f55\u4ece\u5916\u90e8\u8bfb\u53d6\u5c40\u90e8\u53d8\u91cf\uff1f<\/strong><\/p>\n<p>\u51fa\u4e8e\u79cd\u79cd\u539f\u56e0\uff0c\u6211\u4eec\u6709\u65f6\u5019\u9700\u8981\u5f97\u5230\u51fd\u6570\u5185\u7684\u5c40\u90e8\u53d8\u91cf\u3002\u4f46\u662f\uff0c\u524d\u9762\u5df2\u7ecf\u8bf4\u8fc7\u4e86\uff0c\u6b63\u5e38\u60c5\u51b5\u4e0b\uff0c\u8fd9\u662f\u529e\u4e0d\u5230\u7684\uff0c\u53ea\u6709\u901a\u8fc7\u53d8\u901a\u65b9\u6cd5\u624d\u80fd\u5b9e\u73b0\u3002<\/p>\n<p>\u90a3\u5c31\u662f\u5728\u51fd\u6570\u7684\u5185\u90e8\uff0c\u518d\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\u3002<\/p>\n<p>Js\u4ee3\u7801<\/p>\n<p>function f1(){<\/p>\n<p>n=999;<\/p>\n<p>function f2(){<br \/>\nalert(n); \/\/ 999<br \/>\n}<\/p>\n<p>}<\/p>\n<p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u51fd\u6570f2\u5c31\u88ab\u5305\u62ec\u5728\u51fd\u6570f1\u5185\u90e8\uff0c\u8fd9\u65f6f1\u5185\u90e8\u7684\u6240\u6709\u5c40\u90e8\u53d8\u91cf\uff0c\u5bf9f2\u90fd\u662f\u53ef\u89c1\u7684\u3002\u4f46\u662f\u53cd\u8fc7\u6765\u5c31\u4e0d\u884c\uff0cf2\u5185\u90e8\u7684\u5c40\u90e8\u53d8\u91cf\uff0c\u5bf9f1 \u5c31\u662f\u4e0d\u53ef\u89c1\u7684\u3002\u8fd9\u5c31\u662fJavascript\u8bed\u8a00\u7279\u6709\u7684\u201c\u94fe\u5f0f\u4f5c\u7528\u57df\u201d\u7ed3\u6784\uff08chain scope\uff09\uff0c<\/p>\n<p>\u5b50\u5bf9\u8c61\u4f1a\u4e00\u7ea7\u4e00\u7ea7\u5730\u5411\u4e0a\u5bfb\u627e\u6240\u6709\u7236\u5bf9\u8c61\u7684\u53d8\u91cf\u3002\u6240\u4ee5\uff0c\u7236\u5bf9\u8c61\u7684\u6240\u6709\u53d8\u91cf\uff0c\u5bf9\u5b50\u5bf9\u8c61\u90fd\u662f\u53ef\u89c1\u7684\uff0c\u53cd\u4e4b\u5219\u4e0d\u6210\u7acb\u3002<\/p>\n<p>\u65e2\u7136f2\u53ef\u4ee5\u8bfb\u53d6f1\u4e2d\u7684\u5c40\u90e8\u53d8\u91cf\uff0c\u90a3\u4e48\u53ea\u8981\u628af2\u4f5c\u4e3a\u8fd4\u56de\u503c\uff0c\u6211\u4eec\u4e0d\u5c31\u53ef\u4ee5\u5728f1\u5916\u90e8\u8bfb\u53d6\u5b83\u7684\u5185\u90e8\u53d8\u91cf\u4e86\u5417\uff01<br \/>\nJs\u4ee3\u7801<\/p>\n<p>function f1(){<\/p>\n<p>n=999;<\/p>\n<p>function f2(){<br \/>\nalert(n);<br \/>\n}<\/p>\n<p>return f2;<\/p>\n<p>}<\/p>\n<p>var result=f1();<\/p>\n<p>result(); \/\/ 999<\/p>\n<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<\/p>\n<p><strong>\u4e09\u3001\u95ed\u5305\u7684\u6982\u5ff5<\/strong><\/p>\n<p>\u4e0a\u4e00\u8282\u4ee3\u7801\u4e2d\u7684f2\u51fd\u6570\uff0c\u5c31\u662f\u95ed\u5305\u3002<\/p>\n<p>\u5404\u79cd\u4e13\u4e1a\u6587\u732e\u4e0a\u7684\u201c\u95ed\u5305\u201d\uff08closure\uff09\u5b9a\u4e49\u975e\u5e38\u62bd\u8c61\uff0c\u5f88\u96be\u770b\u61c2\u3002\u6211\u7684\u7406\u89e3\u662f\uff0c\u95ed\u5305\u5c31\u662f\u80fd\u591f\u8bfb\u53d6\u5176\u4ed6\u51fd\u6570\u5185\u90e8\u53d8\u91cf\u7684\u51fd\u6570\u3002<\/p>\n<p>\u7531\u4e8e\u5728Javascript\u8bed\u8a00\u4e2d\uff0c\u53ea\u6709\u51fd\u6570\u5185\u90e8\u7684\u5b50\u51fd\u6570\u624d\u80fd\u8bfb\u53d6\u5c40\u90e8\u53d8\u91cf\uff0c\u56e0\u6b64\u53ef\u4ee5\u628a\u95ed\u5305\u7b80\u5355\u7406\u89e3\u6210\u201c\u5b9a\u4e49\u5728\u4e00\u4e2a\u51fd\u6570\u5185\u90e8\u7684\u51fd\u6570\u201d\u3002<\/p>\n<p>\u6240\u4ee5\uff0c\u5728\u672c\u8d28\u4e0a\uff0c\u95ed\u5305\u5c31\u662f\u5c06\u51fd\u6570\u5185\u90e8\u548c\u51fd\u6570\u5916\u90e8\u8fde\u63a5\u8d77\u6765\u7684\u4e00\u5ea7\u6865\u6881\u3002<\/p>\n<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;b<\/p>\n<p><strong>\u56db\u3001\u95ed\u5305\u7684\u7528\u9014<\/strong><\/p>\n<p>\u95ed\u5305\u53ef\u4ee5\u7528\u5728\u8bb8\u591a\u5730\u65b9\u3002\u5b83\u7684\u6700\u5927\u7528\u5904\u6709\u4e24\u4e2a\uff0c\u4e00\u4e2a\u662f\u524d\u9762\u63d0\u5230\u7684\u53ef\u4ee5\u8bfb\u53d6\u51fd\u6570\u5185\u90e8\u7684\u53d8\u91cf\uff0c\u53e6\u4e00\u4e2a\u5c31\u662f\u8ba9\u8fd9\u4e9b\u53d8\u91cf\u7684\u503c\u59cb\u7ec8\u4fdd\u6301\u5728\u5185\u5b58\u4e2d\u3002<\/p>\n<p>\u600e\u4e48\u6765\u7406\u89e3\u8fd9\u53e5\u8bdd\u5462\uff1f\u8bf7\u770b\u4e0b\u9762\u7684\u4ee3\u7801\u3002<br \/>\nJs\u4ee3\u7801<\/p>\n<p>function f1(){<\/p>\n<p>var n=999;<\/p>\n<p>nAdd=function(){n+=1}<\/p>\n<p>function f2(){<br \/>\nalert(n);<br \/>\n}<\/p>\n<p>return f2;<\/p>\n<p>}<\/p>\n<p>var result=f1();<\/p>\n<p>result(); \/\/ 999<\/p>\n<p>nAdd();<\/p>\n<p>result(); \/\/ 1000<\/p>\n<p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0cresult\u5b9e\u9645\u4e0a\u5c31\u662f\u95ed\u5305f2\u51fd\u6570\u3002\u5b83\u4e00\u5171\u8fd0\u884c\u4e86\u4e24\u6b21\uff0c\u7b2c\u4e00\u6b21\u7684\u503c\u662f999\uff0c\u7b2c\u4e8c\u6b21\u7684\u503c\u662f1000\u3002\u8fd9\u8bc1\u660e\u4e86\uff0c\u51fd\u6570f1\u4e2d\u7684\u5c40\u90e8\u53d8\u91cfn\u4e00\u76f4\u4fdd\u5b58\u5728\u5185\u5b58\u4e2d\uff0c\u5e76\u6ca1\u6709\u5728f1\u8c03\u7528\u540e\u88ab\u81ea\u52a8\u6e05\u9664\u3002<\/p>\n<p>\u4e3a\u4ec0\u4e48\u4f1a\u8fd9\u6837\u5462\uff1f\u539f\u56e0\u5c31\u5728\u4e8ef1\u662ff2\u7684\u7236\u51fd\u6570\uff0c\u800cf2\u88ab\u8d4b\u7ed9\u4e86\u4e00\u4e2a\u5168\u5c40\u53d8\u91cf\uff0c\u8fd9\u5bfc\u81f4f2\u59cb\u7ec8\u5728\u5185\u5b58\u4e2d\uff0c\u800cf2\u7684\u5b58\u5728\u4f9d\u8d56\u4e8ef1\uff0c\u56e0\u6b64f1\u4e5f\u59cb\u7ec8\u5728\u5185\u5b58\u4e2d\uff0c\u4e0d\u4f1a\u5728\u8c03\u7528\u7ed3\u675f\u540e\uff0c\u88ab\u5783\u573e\u56de\u6536\u673a\u5236\uff08garbage collection\uff09\u56de\u6536\u3002<\/p>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u4e2d\u53e6\u4e00\u4e2a\u503c\u5f97\u6ce8\u610f\u7684\u5730\u65b9\uff0c\u5c31\u662f\u201cnAdd=function(){n+=1}\u201d\u8fd9\u4e00\u884c\uff0c\u9996\u5148\u5728nAdd\u524d\u9762\u6ca1\u6709\u4f7f\u7528var\u5173\u952e\u5b57\uff0c\u56e0\u6b64 nAdd\u662f\u4e00\u4e2a\u5168\u5c40\u53d8\u91cf\uff0c\u800c\u4e0d\u662f\u5c40\u90e8\u53d8\u91cf\u3002\u5176\u6b21\uff0cnAdd\u7684\u503c\u662f\u4e00\u4e2a\u533f\u540d\u51fd\u6570\uff08anonymous function\uff09\uff0c\u800c\u8fd9\u4e2a<\/p>\n<p>\u533f\u540d\u51fd\u6570\u672c\u8eab\u4e5f\u662f\u4e00\u4e2a\u95ed\u5305\uff0c\u6240\u4ee5nAdd\u76f8\u5f53\u4e8e\u662f\u4e00\u4e2asetter\uff0c\u53ef\u4ee5\u5728\u51fd\u6570\u5916\u90e8\u5bf9\u51fd\u6570\u5185\u90e8\u7684\u5c40\u90e8\u53d8\u91cf\u8fdb\u884c\u64cd\u4f5c\u3002<\/p>\n<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<\/p>\n<p><strong>\u4e94\u3001\u4f7f\u7528\u95ed\u5305\u7684\u6ce8\u610f\u70b9<\/strong><\/p>\n<p>1\uff09\u7531\u4e8e\u95ed\u5305\u4f1a\u4f7f\u5f97\u51fd\u6570\u4e2d\u7684\u53d8\u91cf\u90fd\u88ab\u4fdd\u5b58\u5728\u5185\u5b58\u4e2d\uff0c\u5185\u5b58\u6d88\u8017\u5f88\u5927\uff0c\u6240\u4ee5\u4e0d\u80fd\u6ee5\u7528\u95ed\u5305\uff0c\u5426\u5219\u4f1a\u9020\u6210\u7f51\u9875\u7684\u6027\u80fd\u95ee\u9898\uff0c\u5728IE\u4e2d\u53ef\u80fd\u5bfc\u81f4\u5185\u5b58\u6cc4\u9732\u3002\u89e3\u51b3\u65b9\u6cd5\u662f\uff0c\u5728\u9000\u51fa\u51fd\u6570\u4e4b\u524d\uff0c\u5c06\u4e0d\u4f7f\u7528\u7684\u5c40\u90e8\u53d8\u91cf\u5168\u90e8\u5220\u9664\u3002<\/p>\n<p>2\uff09\u95ed\u5305\u4f1a\u5728\u7236\u51fd\u6570\u5916\u90e8\uff0c\u6539\u53d8\u7236\u51fd\u6570\u5185\u90e8\u53d8\u91cf\u7684\u503c\u3002\u6240\u4ee5\uff0c\u5982\u679c\u4f60\u628a\u7236\u51fd\u6570\u5f53\u4f5c\u5bf9\u8c61\uff08object\uff09\u4f7f\u7528\uff0c\u628a\u95ed\u5305\u5f53\u4f5c\u5b83\u7684\u516c\u7528\u65b9\u6cd5\uff08Public Method\uff09\uff0c\u628a\u5185\u90e8\u53d8\u91cf\u5f53\u4f5c\u5b83\u7684\u79c1\u6709\u5c5e\u6027\uff08private value\uff09\uff0c\u8fd9\u65f6\u4e00\u5b9a\u8981\u5c0f\u5fc3\uff0c\u4e0d\u8981\u968f\u4fbf<\/p>\n<p>\u6539\u53d8\u7236\u51fd\u6570\u5185\u90e8\u53d8\u91cf\u7684\u503c\u3002<\/p>\n<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<\/p>\n<p><strong>\u516d\u3001\u601d\u8003\u9898<\/strong><\/p>\n<p>\u5982\u679c\u4f60\u80fd\u7406\u89e3\u4e0b\u9762\u4ee3\u7801\u7684\u8fd0\u884c\u7ed3\u679c\uff0c\u5e94\u8be5\u5c31\u7b97\u7406\u89e3\u95ed\u5305\u7684\u8fd0\u884c\u673a\u5236\u4e86\u3002<\/p>\n<p>Js\u4ee3\u7801<br \/>\nvar name = &#8220;The Window&#8221;;<br \/>\nvar object = {<br \/>\nname : &#8220;My Object&#8221;,<br \/>\ngetNameFunc : function(){<br \/>\nreturn function(){<br \/>\nreturn this.name;<br \/>\n};<br \/>\n}<br \/>\n};<br \/>\nalert(object.getNameFunc()());\u00a0 \/\/The Window<\/p>\n<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<br \/>\n<strong>JavaScript\u95ed\u5305\u4f8b\u5b50<\/strong><\/p>\n<p>function outerFun()<br \/>\n{<br \/>\nvar a=0;<br \/>\nfunction innerFun()<br \/>\n{<br \/>\na++;<br \/>\nalert(a);<br \/>\n}<br \/>\n}<br \/>\ninnerFun()<\/p>\n<p>\u4e0a\u9762\u7684\u4ee3\u7801\u662f\u9519\u8bef\u7684.innerFun()\u7684\u4f5c\u7528\u57df\u5728outerFun()\u5185\u90e8,\u6240\u5728outerFun()\u5916\u90e8\u8c03\u7528\u5b83\u662f\u9519\u8bef\u7684.<\/p>\n<p>\u6539\u6210\u5982\u4e0b,\u4e5f\u5c31\u662f\u95ed\u5305:<\/p>\n<p>Js\u4ee3\u7801<\/p>\n<p>function outerFun()<br \/>\n{<br \/>\nvar a=0;<br \/>\nfunction innerFun()<br \/>\n{<br \/>\na++;<br \/>\nalert(a);<br \/>\n}<br \/>\nreturn innerFun;\u00a0 \/\/\u6ce8\u610f\u8fd9\u91cc<br \/>\n}<br \/>\nvar obj=outerFun();<br \/>\nobj();\u00a0 \/\/\u7ed3\u679c\u4e3a1<br \/>\nobj();\u00a0 \/\/\u7ed3\u679c\u4e3a2<br \/>\nvar obj2=outerFun();<br \/>\nobj2();\u00a0 \/\/\u7ed3\u679c\u4e3a1<br \/>\nobj2();\u00a0 \/\/\u7ed3\u679c\u4e3a2<\/p>\n<p>\u4ec0\u4e48\u662f\u95ed\u5305:<\/p>\n<p>\u5f53\u5185\u90e8\u51fd\u6570 \u5728\u5b9a\u4e49\u5b83\u7684\u4f5c\u7528\u57df \u7684\u5916\u90e8 \u88ab\u5f15\u7528\u65f6,\u5c31\u521b\u5efa\u4e86\u8be5\u5185\u90e8\u51fd\u6570\u7684\u95ed\u5305 ,\u5982\u679c\u5185\u90e8\u51fd\u6570\u5f15\u7528\u4e86\u4f4d\u4e8e\u5916\u90e8\u51fd\u6570\u7684\u53d8\u91cf,\u5f53\u5916\u90e8\u51fd\u6570\u8c03\u7528\u5b8c\u6bd5\u540e,\u8fd9\u4e9b\u53d8\u91cf\u5728\u5185\u5b58\u4e0d\u4f1a\u88ab \u91ca\u653e,\u56e0\u4e3a\u95ed\u5305\u9700\u8981\u5b83\u4eec.<\/p>\n<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<\/p>\n<p><strong>\u518d\u6765\u770b\u4e00\u4e2a\u4f8b\u5b50<\/strong><\/p>\n<p>Js\u4ee3\u7801<\/p>\n<p>function outerFun()<br \/>\n{<br \/>\nvar a =0;<br \/>\nalert(a);<br \/>\n}<br \/>\nvar a=4;<br \/>\nouterFun();<br \/>\nalert(a);<\/p>\n<p>\u7ed3\u679c\u662f 0,4 .\u00a0 \u56e0\u4e3a\u5728\u51fd\u6570\u5185\u90e8\u4f7f\u7528\u4e86var\u5173\u952e\u5b57 \u7ef4\u62a4a\u7684\u4f5c\u7528\u57df\u5728outFun()\u5185\u90e8.<\/p>\n<p>\u518d\u770b\u4e0b\u9762\u7684\u4ee3\u7801:<\/p>\n<p>Js\u4ee3\u7801<\/p>\n<p>function outerFun()<br \/>\n{<br \/>\n\/\/\u6ca1\u6709var<br \/>\na =0;<br \/>\nalert(a);<br \/>\n}<br \/>\nvar a=4;<br \/>\nouterFun();<br \/>\nalert(a);<br \/>\n\u7ed3\u679c\u4e3a 0,0 \u771f\u662f\u5947\u602a,\u4e3a\u4ec0\u4e48\u5462?<\/p>\n<p>\u4f5c\u7528\u57df\u94fe\u662f\u63cf\u8ff0\u4e00\u79cd\u8def\u5f84\u7684\u672f\u8bed,\u6cbf\u7740\u8be5\u8def\u5f84\u53ef\u4ee5\u786e\u5b9a\u53d8\u91cf\u7684\u503c .\u5f53\u6267\u884ca=0\u65f6,\u56e0\u4e3a\u6ca1\u6709\u4f7f\u7528var\u5173\u952e\u5b57,\u56e0\u6b64\u8d4b\u503c\u64cd\u4f5c\u4f1a\u6cbf\u7740\u4f5c\u7528\u57df\u94fe\u5230var a=4;\u00a0 \u5e76\u6539\u53d8\u5176\u503c.<\/p>\n<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<\/p>\n<p>\u5982\u679c\u4f60\u5bf9javascript\u95ed\u5305\u8fd8\u4e0d\u662f\u5f88\u7406\u89e3\uff0c\u90a3\u4e48\u8bf7\u770b\u4e0b\u9762\u8f6c\u8f7d\u7684\u6587\u7ae0\uff1a(\u8f6c\u8f7d:http:\/\/www.felixwoo.com\/archives\/247)<br \/>\n<strong>\u4e00\u3001\u4ec0\u4e48\u662f\u95ed\u5305\uff1f<\/strong><\/p>\n<p>\u5b98\u65b9\u201d\u7684\u89e3\u91ca\u662f\uff1a\u95ed\u5305\u662f\u4e00\u4e2a\u62e5\u6709\u8bb8\u591a\u53d8\u91cf\u548c\u7ed1\u5b9a\u4e86\u8fd9\u4e9b\u53d8\u91cf\u7684\u73af\u5883\u7684\u8868\u8fbe\u5f0f\uff08\u901a\u5e38\u662f\u4e00\u4e2a\u51fd\u6570\uff09\uff0c\u56e0\u800c\u8fd9\u4e9b\u53d8\u91cf\u4e5f\u662f\u8be5\u8868\u8fbe\u5f0f\u7684\u4e00\u90e8\u5206\u3002<br \/>\n\u76f8\u4fe1\u5f88\u5c11\u6709\u4eba\u80fd\u76f4\u63a5\u770b\u61c2\u8fd9\u53e5\u8bdd\uff0c\u56e0\u4e3a\u4ed6\u63cf\u8ff0\u7684\u592a\u5b66\u672f\u3002\u5176\u5b9e\u8fd9\u53e5\u8bdd\u901a\u4fd7\u7684\u6765\u8bf4\u5c31\u662f\uff1a<strong>JavaScript\u4e2d\u6240\u6709\u7684function\u90fd\u662f\u4e00\u4e2a\u95ed\u5305<\/strong>\u3002\u4e0d\u8fc7\u4e00\u822c\u6765\u8bf4\uff0c\u5d4c\u5957\u7684function\u6240\u4ea7\u751f\u7684\u95ed\u5305\u66f4\u4e3a\u5f3a\u5927\uff0c\u4e5f\u662f\u5927\u90e8\u5206\u65f6\u5019\u6211\u4eec\u6240\u8c13\u7684\u201c\u95ed\u5305\u201d\u3002\u770b\u4e0b\u9762\u8fd9\u6bb5\u4ee3\u7801\uff1a<\/p>\n<pre class=\"hljs actionscript\"><strong><span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span><\/span><\/strong><span class=\"hljs-function\"> <span class=\"hljs-title\">a<\/span><\/span><span class=\"hljs-function\"><span class=\"hljs-params\">(<\/span><\/span><span class=\"hljs-function\"><span class=\"hljs-params\">)<\/span><\/span> { \n <strong><span class=\"hljs-keyword\">var<\/span><\/strong> i = <span class=\"hljs-number\">0<\/span>; \n <strong><span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span><\/span><\/strong><span class=\"hljs-function\"> <span class=\"hljs-title\">b<\/span><\/span><span class=\"hljs-function\"><span class=\"hljs-params\">(<\/span><\/span><span class=\"hljs-function\"><span class=\"hljs-params\">)<\/span><\/span> { alert(++i); } \n <strong><span class=\"hljs-keyword\">return<\/span><\/strong> b;\n}\n<strong><span class=\"hljs-keyword\">var<\/span><\/strong> c = a();\nc();<\/pre>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u6709\u4e24\u4e2a\u7279\u70b9\uff1a<\/p>\n<p>1\u3001\u51fd\u6570b\u5d4c\u5957\u5728\u51fd\u6570a\u5185\u90e8\uff1b<\/p>\n<p>2\u3001\u51fd\u6570a\u8fd4\u56de\u51fd\u6570b\u3002<\/p>\n<p>\u5f15\u7528\u5173\u7cfb\u5982\u56fe\uff1a<\/p>\n<p><img src=\"https:\/\/static.oschina.net\/uploads\/img\/201701\/12115202_Gwp1.jpg\" \/><\/p>\n<p>\u8fd9\u6837\u5728\u6267\u884c\u5b8cvar c=a()\u540e\uff0c\u53d8\u91cfc\u5b9e\u9645\u4e0a\u662f\u6307\u5411\u4e86\u51fd\u6570b\uff0c\u518d\u6267\u884cc()\u540e\u5c31\u4f1a\u5f39\u51fa\u4e00\u4e2a\u7a97\u53e3\u663e\u793ai\u7684\u503c(\u7b2c\u4e00\u6b21\u4e3a1)\u3002\u8fd9\u6bb5\u4ee3\u7801\u5176\u5b9e\u5c31\u521b\u5efa\u4e86\u4e00\u4e2a\u95ed\u5305\uff0c\u4e3a\u4ec0\u4e48\uff1f\u56e0\u4e3a\u51fd\u6570a\u5916\u7684\u53d8\u91cfc\u5f15\u7528\u4e86\u51fd\u6570a\u5185\u7684\u51fd\u6570b\uff0c\u5c31\u662f\u8bf4\uff1a<\/p>\n<p><strong>\u5f53\u51fd\u6570a\u7684\u5185\u90e8\u51fd\u6570b\u88ab\u51fd\u6570a\u5916\u7684\u4e00\u4e2a\u53d8\u91cf\u5f15\u7528\u7684\u65f6\u5019\uff0c\u5c31\u521b\u5efa\u4e86\u4e00\u4e2a\u95ed\u5305\u3002<\/strong><\/p>\n<p>\u8ba9\u6211\u4eec\u8bf4\u7684\u66f4\u900f\u5f7b\u4e00\u4e9b\u3002\u6240\u8c13\u201c\u95ed\u5305\u201d\uff0c\u5c31\u662f\u5728\u6784\u9020\u51fd\u6570\u4f53\u5185\u5b9a\u4e49\u53e6\u5916\u7684\u51fd\u6570\u4f5c\u4e3a\u76ee\u6807\u5bf9\u8c61\u7684\u65b9\u6cd5\u51fd\u6570\uff0c\u800c\u8fd9\u4e2a\u5bf9\u8c61\u7684\u65b9\u6cd5\u51fd\u6570\u53cd\u8fc7\u6765\u5f15\u7528\u5916\u5c42\u51fd\u6570\u4f53\u4e2d\u7684\u4e34\u65f6\u53d8\u91cf\u3002\u8fd9\u4f7f\u5f97\u53ea\u8981\u76ee\u6807 \u5bf9\u8c61\u5728\u751f\u5b58\u671f\u5185\u59cb\u7ec8\u80fd\u4fdd\u6301\u5176\u65b9\u6cd5\uff0c\u5c31\u80fd\u95f4\u63a5\u4fdd\u6301\u539f\u6784\u9020\u51fd\u6570\u4f53\u5f53\u65f6\u7528\u5230\u7684\u4e34\u65f6\u53d8\u91cf\u503c\u3002\u5c3d\u7ba1\u6700\u5f00\u59cb\u7684\u6784\u9020\u51fd\u6570\u8c03\u7528\u5df2\u7ecf\u7ed3\u675f\uff0c\u4e34\u65f6\u53d8\u91cf\u7684\u540d\u79f0\u4e5f\u90fd\u6d88\u5931\u4e86\uff0c\u4f46\u5728\u76ee \u6807\u5bf9\u8c61\u7684\u65b9\u6cd5\u5185\u5374\u59cb\u7ec8\u80fd\u5f15\u7528\u5230\u8be5\u53d8\u91cf\u7684\u503c\uff0c\u800c\u4e14\u8be5\u503c\u53ea\u80fd\u901a\u8fd9\u79cd\u65b9\u6cd5\u6765\u8bbf\u95ee\u3002\u5373\u4f7f\u518d\u6b21\u8c03\u7528\u76f8\u540c\u7684\u6784\u9020\u51fd\u6570\uff0c\u4f46\u53ea\u4f1a\u751f\u6210\u65b0\u5bf9\u8c61\u548c\u65b9\u6cd5\uff0c\u65b0\u7684\u4e34\u65f6\u53d8\u91cf\u53ea\u662f\u5bf9\u5e94\u65b0 \u7684\u503c\uff0c\u548c\u4e0a\u6b21\u90a3\u6b21\u8c03\u7528\u7684\u662f\u5404\u81ea\u72ec\u7acb\u7684\u3002<\/p>\n<p><strong>\u4e8c\u3001\u95ed\u5305\u6709\u4ec0\u4e48\u4f5c\u7528\uff1f<\/strong><\/p>\n<p>\u7b80\u800c\u8a00\u4e4b\uff0c\u95ed\u5305\u7684\u4f5c\u7528\u5c31\u662f\u5728a\u6267\u884c\u5b8c\u5e76\u8fd4\u56de\u540e\uff0c\u95ed\u5305\u4f7f\u5f97Javascript\u7684\u5783\u573e\u56de\u6536\u673a\u5236GC\u4e0d\u4f1a\u6536\u56dea\u6240\u5360\u7528\u7684\u8d44\u6e90\uff0c\u56e0\u4e3aa\u7684\u5185\u90e8\u51fd\u6570b\u7684\u6267\u884c\u9700\u8981\u4f9d\u8d56a\u4e2d\u7684\u53d8\u91cf\u3002\u8fd9\u662f\u5bf9\u95ed\u5305\u4f5c\u7528\u7684\u975e\u5e38\u76f4\u767d\u7684\u63cf\u8ff0\uff0c\u4e0d\u4e13\u4e1a\u4e5f\u4e0d\u4e25\u8c28\uff0c\u4f46\u5927\u6982\u610f\u601d\u5c31\u662f\u8fd9\u6837\uff0c\u7406\u89e3\u95ed\u5305\u9700\u8981\u5faa\u5e8f\u6e10\u8fdb\u7684\u8fc7\u7a0b\u3002<\/p>\n<p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u7531\u4e8e\u95ed\u5305\u7684\u5b58\u5728\u4f7f\u5f97\u51fd\u6570a\u8fd4\u56de\u540e\uff0ca\u4e2d\u7684i\u59cb\u7ec8\u5b58\u5728\uff0c\u8fd9\u6837\u6bcf\u6b21\u6267\u884cc()\uff0ci\u90fd\u662f\u81ea\u52a01\u540ealert\u51fai\u7684\u503c\u3002<\/p>\n<p>\u90a3 \u4e48\u6211\u4eec\u6765\u60f3\u8c61\u53e6\u4e00\u79cd\u60c5\u51b5\uff0c\u5982\u679ca\u8fd4\u56de\u7684\u4e0d\u662f\u51fd\u6570b\uff0c\u60c5\u51b5\u5c31\u5b8c\u5168\u4e0d\u540c\u4e86\u3002\u56e0\u4e3aa\u6267\u884c\u5b8c\u540e\uff0cb\u6ca1\u6709\u88ab\u8fd4\u56de\u7ed9a\u7684\u5916\u754c\uff0c\u53ea\u662f\u88aba\u6240\u5f15\u7528\uff0c\u800c\u6b64\u65f6a\u4e5f\u53ea\u4f1a\u88abb\u5f15 \u7528\uff0c\u56e0\u6b64\u51fd\u6570a\u548cb\u4e92\u76f8\u5f15\u7528\u4f46\u53c8\u4e0d\u88ab\u5916\u754c\u6253\u6270(\u88ab\u5916\u754c\u5f15\u7528)\uff0c\u51fd\u6570a\u548cb\u5c31\u4f1a\u88abGC\u56de\u6536\u3002(\u5173\u4e8eJavascript\u7684\u5783\u573e\u56de\u6536\u673a\u5236\u5c06\u5728\u540e\u9762\u8be6\u7ec6\u4ecb\u7ecd)<\/p>\n<p><strong>\u4e09\u3001\u95ed\u5305\u5185\u7684\u5fae\u89c2\u4e16\u754c<\/strong><\/p>\n<p>\u5982\u679c\u8981\u66f4\u52a0\u6df1\u5165\u7684\u4e86\u89e3\u95ed\u5305\u4ee5\u53ca\u51fd\u6570a\u548c\u5d4c\u5957\u51fd\u6570b\u7684\u5173\u7cfb\uff0c\u6211\u4eec\u9700\u8981\u5f15\u5165\u53e6\u5916\u51e0\u4e2a\u6982\u5ff5\uff1a\u51fd\u6570\u7684\u6267\u884c\u73af\u5883(excution context)\u3001\u6d3b\u52a8\u5bf9\u8c61(call object)\u3001\u4f5c\u7528\u57df(scope)\u3001\u4f5c\u7528\u57df\u94fe(scope chain)\u3002\u4ee5\u51fd\u6570a\u4ece\u5b9a\u4e49\u5230\u6267\u884c\u7684\u8fc7\u7a0b\u4e3a\u4f8b\u9610\u8ff0\u8fd9\u51e0\u4e2a\u6982\u5ff5\u3002<\/p>\n<ol>\n<li>\u5f53<strong>\u5b9a\u4e49<\/strong>\u51fd\u6570a\u7684\u65f6\u5019\uff0cjs\u89e3\u91ca\u5668\u4f1a\u5c06\u51fd\u6570a\u7684<strong>\u4f5c\u7528\u57df\u94fe(scope chain)<\/strong>\u8bbe\u7f6e\u4e3a<strong>\u5b9a\u4e49a\u65f6a\u6240\u5728\u7684\u201c\u73af\u5883\u201d<\/strong>\uff0c\u5982\u679ca\u662f\u4e00\u4e2a\u5168\u5c40\u51fd\u6570\uff0c\u5219scope chain\u4e2d\u53ea\u6709window\u5bf9\u8c61\u3002<\/li>\n<li>\u5f53<strong>\u6267\u884c<\/strong>\u51fd\u6570a\u7684\u65f6\u5019\uff0ca\u4f1a\u8fdb\u5165\u76f8\u5e94\u7684<strong>\u6267\u884c\u73af\u5883(excution context)<\/strong>\u3002<\/li>\n<li>\u5728\u521b\u5efa\u6267\u884c\u73af\u5883\u7684\u8fc7\u7a0b\u4e2d\uff0c\u9996\u5148\u4f1a\u4e3aa\u6dfb\u52a0\u4e00\u4e2ascope\u5c5e\u6027\uff0c\u5373a\u7684<strong>\u4f5c\u7528\u57df<\/strong>\uff0c\u5176\u503c\u5c31\u4e3a\u7b2c1\u6b65\u4e2d\u7684scope chain\u3002\u5373a.scope=a\u7684\u4f5c\u7528\u57df\u94fe\u3002<\/li>\n<li>\u7136\u540e\u6267\u884c\u73af\u5883\u4f1a\u521b\u5efa\u4e00\u4e2a<strong>\u6d3b\u52a8\u5bf9\u8c61(call object)<\/strong>\u3002\u6d3b\u52a8\u5bf9\u8c61\u4e5f\u662f\u4e00\u4e2a\u62e5\u6709\u5c5e\u6027\u7684\u5bf9\u8c61\uff0c\u4f46\u5b83\u4e0d\u5177\u6709\u539f\u578b\u800c\u4e14\u4e0d\u80fd\u901a\u8fc7JavaScript\u4ee3\u7801\u76f4\u63a5\u8bbf\u95ee\u3002\u521b\u5efa\u5b8c\u6d3b\u52a8\u5bf9\u8c61\u540e\uff0c\u628a\u6d3b\u52a8\u5bf9\u8c61\u6dfb\u52a0\u5230a\u7684\u4f5c\u7528\u57df\u94fe\u7684\u6700\u9876\u7aef\u3002\u6b64\u65f6a\u7684\u4f5c\u7528\u57df\u94fe\u5305\u542b\u4e86\u4e24\u4e2a\u5bf9\u8c61\uff1aa\u7684\u6d3b\u52a8\u5bf9\u8c61\u548cwindow\u5bf9\u8c61\u3002<\/li>\n<li>\u4e0b\u4e00\u6b65\u662f\u5728\u6d3b\u52a8\u5bf9\u8c61\u4e0a\u6dfb\u52a0\u4e00\u4e2aarguments\u5c5e\u6027\uff0c\u5b83\u4fdd\u5b58\u7740\u8c03\u7528\u51fd\u6570a\u65f6\u6240\u4f20\u9012\u7684\u53c2\u6570\u3002<\/li>\n<li>\u6700\u540e\u628a\u6240\u6709\u51fd\u6570a\u7684\u5f62\u53c2\u548c\u5185\u90e8\u7684\u51fd\u6570b\u7684\u5f15\u7528\u4e5f\u6dfb\u52a0\u5230a\u7684\u6d3b\u52a8\u5bf9\u8c61\u4e0a\u3002\u5728\u8fd9\u4e00\u6b65\u4e2d\uff0c\u5b8c\u6210\u4e86\u51fd\u6570b\u7684\u7684\u5b9a\u4e49\uff0c\u56e0\u6b64\u5982\u540c\u7b2c3\u6b65\uff0c\u51fd\u6570b\u7684\u4f5c\u7528\u57df\u94fe\u88ab\u8bbe\u7f6e\u4e3ab\u6240\u88ab\u5b9a\u4e49\u7684\u73af\u5883\uff0c\u5373a\u7684\u4f5c\u7528\u57df\u3002<\/li>\n<\/ol>\n<p>\u5230\u6b64\uff0c\u6574\u4e2a\u51fd\u6570a\u4ece\u5b9a\u4e49\u5230\u6267\u884c\u7684\u6b65\u9aa4\u5c31\u5b8c\u6210\u4e86\u3002\u6b64\u65f6a\u8fd4\u56de\u51fd\u6570b\u7684\u5f15\u7528\u7ed9c\uff0c\u53c8\u51fd\u6570b\u7684\u4f5c\u7528\u57df\u94fe\u5305\u542b\u4e86\u5bf9\u51fd\u6570a\u7684\u6d3b\u52a8\u5bf9\u8c61\u7684\u5f15\u7528\uff0c\u4e5f\u5c31\u662f\u8bf4b\u53ef\u4ee5\u8bbf\u95ee\u5230a\u4e2d\u5b9a\u4e49\u7684\u6240\u6709\u53d8\u91cf\u548c\u51fd\u6570\u3002\u51fd\u6570b\u88abc\u5f15\u7528\uff0c\u51fd\u6570b\u53c8\u4f9d\u8d56\u51fd\u6570a\uff0c\u56e0\u6b64\u51fd\u6570a\u5728\u8fd4\u56de\u540e\u4e0d\u4f1a\u88abGC\u56de\u6536\u3002<\/p>\n<p>\u5f53\u51fd\u6570b\u6267\u884c\u7684\u65f6\u5019\u4ea6\u4f1a\u50cf\u4ee5\u4e0a\u6b65\u9aa4\u4e00\u6837\u3002\u56e0\u6b64\uff0c\u6267\u884c\u65f6b\u7684\u4f5c\u7528\u57df\u94fe\u5305\u542b\u4e863\u4e2a\u5bf9\u8c61\uff1ab\u7684\u6d3b\u52a8\u5bf9\u8c61\u3001a\u7684\u6d3b\u52a8\u5bf9\u8c61\u548cwindow\u5bf9\u8c61\uff0c\u5982\u4e0b\u56fe\u6240\u793a\uff1a<\/p>\n<p><img src=\"https:\/\/static.oschina.net\/uploads\/img\/201701\/12115203_PBXs.jpg\" \/><\/p>\n<p>\u5982\u56fe\u6240\u793a\uff0c\u5f53\u5728\u51fd\u6570b\u4e2d\u8bbf\u95ee\u4e00\u4e2a\u53d8\u91cf\u7684\u65f6\u5019\uff0c\u641c\u7d22\u987a\u5e8f\u662f\uff1a<\/p>\n<ol>\n<li>\u5148\u641c\u7d22\u81ea\u8eab\u7684\u6d3b\u52a8\u5bf9\u8c61\uff0c\u5982\u679c\u5b58\u5728\u5219\u8fd4\u56de\uff0c\u5982\u679c\u4e0d\u5b58\u5728\u5c06\u7ee7\u7eed\u641c\u7d22\u51fd\u6570a\u7684\u6d3b\u52a8\u5bf9\u8c61\uff0c\u4f9d\u6b21\u67e5\u627e\uff0c\u76f4\u5230\u627e\u5230\u4e3a\u6b62\u3002<\/li>\n<li>\u5982\u679c\u51fd\u6570b\u5b58\u5728prototype\u539f\u578b\u5bf9\u8c61\uff0c\u5219\u5728\u67e5\u627e\u5b8c\u81ea\u8eab\u7684\u6d3b\u52a8\u5bf9\u8c61\u540e\u5148\u67e5\u627e\u81ea\u8eab\u7684\u539f\u578b\u5bf9\u8c61\uff0c\u518d\u7ee7\u7eed\u67e5\u627e\u3002\u8fd9\u5c31\u662fJavascript\u4e2d\u7684\u53d8\u91cf\u67e5\u627e\u673a\u5236\u3002<\/li>\n<li>\u5982\u679c\u6574\u4e2a\u4f5c\u7528\u57df\u94fe\u4e0a\u90fd\u65e0\u6cd5\u627e\u5230\uff0c\u5219\u8fd4\u56deundefined\u3002<\/li>\n<\/ol>\n<p>\u5c0f\u7ed3\uff0c\u672c\u6bb5\u4e2d\u63d0\u5230\u4e86\u4e24\u4e2a\u91cd\u8981\u7684\u8bcd\u8bed\uff1a\u51fd\u6570\u7684<strong>\u5b9a\u4e49<\/strong>\u4e0e<strong>\u6267\u884c<\/strong>\u3002\u6587\u4e2d\u63d0\u5230\u51fd\u6570\u7684\u4f5c\u7528\u57df\u662f\u5728\u5b9a\u4e49\u51fd\u6570\u65f6\u5019\u5c31\u5df2\u7ecf\u786e\u5b9a\uff0c\u800c\u4e0d\u662f\u5728\u6267\u884c\u7684\u65f6\u5019\u786e\u5b9a\uff08\u53c2\u770b\u6b65\u9aa41\u548c3\uff09\u3002\u7528\u4e00\u6bb5\u4ee3\u7801\u6765\u8bf4\u660e\u8fd9\u4e2a\u95ee\u9898\uff1a<\/p>\n<pre class=\"hljs actionscript\"><strong><span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span><\/span><\/strong><span class=\"hljs-function\"> <span class=\"hljs-title\">f<\/span><\/span><span class=\"hljs-function\"><span class=\"hljs-params\">(<\/span><\/span><span class=\"hljs-function\"><span class=\"hljs-params\">x<\/span><\/span><span class=\"hljs-function\"><span class=\"hljs-params\">)<\/span><\/span> { \n  <strong><span class=\"hljs-keyword\">var<\/span><\/strong> g = <strong><span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span><\/span><\/strong> <span class=\"hljs-function\"><span class=\"hljs-params\">(<\/span><\/span><span class=\"hljs-function\"><span class=\"hljs-params\">)<\/span><\/span> { <strong><span class=\"hljs-keyword\">return<\/span><\/strong> x; }\n  <strong><span class=\"hljs-keyword\">return<\/span><\/strong> g;\n}\n<strong><span class=\"hljs-keyword\">var<\/span><\/strong> h = f(<span class=\"hljs-number\">1<\/span>);\nalert(h());<\/pre>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u4e2d\u53d8\u91cfh\u6307\u5411\u4e86f\u4e2d\u7684\u90a3\u4e2a\u533f\u540d\u51fd\u6570(\u7531g\u8fd4\u56de)\u3002<\/p>\n<ul>\n<li>\u5047\u8bbe\u51fd\u6570h\u7684\u4f5c\u7528\u57df\u662f\u5728\u6267\u884calert(h())\u786e\u5b9a\u7684\uff0c\u90a3\u4e48\u6b64\u65f6h\u7684\u4f5c\u7528\u57df\u94fe\u662f\uff1ah\u7684\u6d3b\u52a8\u5bf9\u8c61-&gt;alert\u7684\u6d3b\u52a8\u5bf9\u8c61-&gt;window\u5bf9\u8c61\u3002<\/li>\n<li>\u5047\u8bbe\u51fd\u6570h\u7684\u4f5c\u7528\u57df\u662f\u5728\u5b9a\u4e49\u65f6\u786e\u5b9a\u7684\uff0c\u5c31\u662f\u8bf4h\u6307\u5411\u7684\u90a3\u4e2a\u533f\u540d\u51fd\u6570\u5728\u5b9a\u4e49\u7684\u65f6\u5019\u5c31\u5df2\u7ecf\u786e\u5b9a\u4e86\u4f5c\u7528\u57df\u3002\u90a3\u4e48\u5728\u6267\u884c\u7684\u65f6\u5019\uff0ch\u7684\u4f5c\u7528\u57df\u94fe\u4e3a\uff1ah\u7684\u6d3b\u52a8\u5bf9\u8c61-&gt;f\u7684\u6d3b\u52a8\u5bf9\u8c61-&gt;window\u5bf9\u8c61\u3002<\/li>\n<\/ul>\n<p>\u5982\u679c\u7b2c\u4e00\u79cd\u5047\u8bbe\u6210\u7acb\uff0c\u90a3\u8f93\u51fa\u503c\u5c31\u662fundefined\uff1b\u5982\u679c\u7b2c\u4e8c\u79cd\u5047\u8bbe\u6210\u7acb\uff0c\u8f93\u51fa\u503c\u5219\u4e3a1\u3002<\/p>\n<p>\u8fd0\u884c\u7ed3\u679c\u8bc1\u660e\u4e86\u7b2c2\u4e2a\u5047\u8bbe\u662f\u6b63\u786e\u7684\uff0c\u8bf4\u660e\u51fd\u6570\u7684\u4f5c\u7528\u57df\u786e\u5b9e\u662f\u5728\u5b9a\u4e49\u8fd9\u4e2a\u51fd\u6570\u7684\u65f6\u5019\u5c31\u5df2\u7ecf\u786e\u5b9a\u4e86\u3002<br \/>\n<strong>\u56db\u3001\u95ed\u5305\u7684\u5e94\u7528\u573a\u666f<\/strong><br \/>\n\u4fdd\u62a4\u51fd\u6570\u5185\u7684\u53d8\u91cf\u5b89\u5168\u3002\u4ee5\u6700\u5f00\u59cb\u7684\u4f8b\u5b50\u4e3a\u4f8b\uff0c\u51fd\u6570a\u4e2di\u53ea\u6709\u51fd\u6570b\u624d\u80fd\u8bbf\u95ee\uff0c\u800c\u65e0\u6cd5\u901a\u8fc7\u5176\u4ed6\u9014\u5f84\u8bbf\u95ee\u5230\uff0c\u56e0\u6b64\u4fdd\u62a4\u4e86i\u7684\u5b89\u5168\u6027\u3002<\/p>\n<ol>\n<li>\u5728\u5185\u5b58\u4e2d\u7ef4\u6301\u4e00\u4e2a\u53d8\u91cf\u3002\u4f9d\u7136\u5982\u524d\u4f8b\uff0c\u7531\u4e8e\u95ed\u5305\uff0c\u51fd\u6570a\u4e2di\u7684\u4e00\u76f4\u5b58\u5728\u4e8e\u5185\u5b58\u4e2d\uff0c\u56e0\u6b64\u6bcf\u6b21\u6267\u884cc()\uff0c\u90fd\u4f1a\u7ed9i\u81ea\u52a01\u3002<\/li>\n<li>\u901a\u8fc7\u4fdd\u62a4\u53d8\u91cf\u7684\u5b89\u5168\u5b9e\u73b0JS\u79c1\u6709\u5c5e\u6027\u548c\u79c1\u6709\u65b9\u6cd5\uff08\u4e0d\u80fd\u88ab\u5916\u90e8\u8bbf\u95ee\uff09<br \/>\n\u79c1\u6709\u5c5e\u6027\u548c\u65b9\u6cd5\u5728Constructor\u5916\u662f\u65e0\u6cd5\u88ab\u8bbf\u95ee\u7684<strong>function<\/strong> Constructor(&#8230;) {<br \/>\n<strong>var<\/strong> that = <strong>this<\/strong>;<br \/>\n<strong>var<\/strong> membername = value;<br \/>\n<strong>function<\/strong> membername(&#8230;) {&#8230;}<br \/>\n}<\/li>\n<\/ol>\n<p>\u4ee5\u4e0a3\u70b9\u662f\u95ed\u5305\u6700\u57fa\u672c\u7684\u5e94\u7528\u573a\u666f\uff0c\u5f88\u591a\u7ecf\u5178\u6848\u4f8b\u90fd\u6e90\u4e8e\u6b64\u3002<br \/>\n<strong>\u4e94\u3001Javascript\u7684\u5783\u573e\u56de\u6536\u673a\u5236<\/strong><\/p>\n<p>\u5728Javascript\u4e2d\uff0c\u5982\u679c\u4e00\u4e2a\u5bf9\u8c61\u4e0d\u518d\u88ab\u5f15\u7528\uff0c\u90a3\u4e48\u8fd9\u4e2a\u5bf9\u8c61\u5c31\u4f1a\u88abGC\u56de\u6536\u3002\u5982\u679c\u4e24\u4e2a\u5bf9\u8c61\u4e92\u76f8\u5f15\u7528\uff0c\u800c\u4e0d\u518d\u88ab\u7b2c3\u8005\u6240\u5f15\u7528\uff0c\u90a3\u4e48\u8fd9\u4e24\u4e2a\u4e92\u76f8\u5f15\u7528\u7684\u5bf9\u8c61\u4e5f\u4f1a\u88ab\u56de\u6536\u3002\u56e0\u4e3a\u51fd\u6570a\u88abb\u5f15\u7528\uff0cb\u53c8\u88aba\u5916\u7684c\u5f15\u7528\uff0c\u8fd9\u5c31\u662f\u4e3a\u4ec0\u4e48\u51fd\u6570a\u6267\u884c\u540e\u4e0d\u4f1a\u88ab\u56de\u6536\u7684\u539f\u56e0\u3002<\/p>\n<p><strong>\u516d\u3001\u7ed3\u8bed<\/strong><\/p>\n<p>\u7406\u89e3JavaScript\u7684\u95ed\u5305\u662f\u8fc8\u5411\u9ad8\u7ea7JS\u7a0b\u5e8f\u5458\u7684\u5fc5\u7ecf\u4e4b\u8def\uff0c\u7406\u89e3\u4e86\u5176\u89e3\u91ca\u548c\u8fd0\u884c\u673a\u5236\u624d\u80fd\u5199\u51fa\u66f4\u4e3a\u5b89\u5168\u548c\u4f18\u96c5\u7684\u4ee3\u7801\u3002<\/p>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u6458\u8981: \u95ed\u5305\uff08closure\uff09\u662fJavascript\u8bed\u8a00\u7684\u4e00\u4e2a\u96be\u70b9\uff0c\u4e5f\u662f\u5b83\u7684\u7279\u8272\uff0c\u5f88\u591a\u9ad8\u7ea7\u5e94\u7528\u90fd\u8981\u4f9d\u9760\u95ed\u5305\u5b9e\u73b0 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":180,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[10],"tags":[19],"_links":{"self":[{"href":"http:\/\/blog.xrhoujie.com\/index.php?rest_route=\/wp\/v2\/posts\/120"}],"collection":[{"href":"http:\/\/blog.xrhoujie.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/blog.xrhoujie.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/blog.xrhoujie.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/blog.xrhoujie.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=120"}],"version-history":[{"count":4,"href":"http:\/\/blog.xrhoujie.com\/index.php?rest_route=\/wp\/v2\/posts\/120\/revisions"}],"predecessor-version":[{"id":1026,"href":"http:\/\/blog.xrhoujie.com\/index.php?rest_route=\/wp\/v2\/posts\/120\/revisions\/1026"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/blog.xrhoujie.com\/index.php?rest_route=\/wp\/v2\/media\/180"}],"wp:attachment":[{"href":"http:\/\/blog.xrhoujie.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=120"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/blog.xrhoujie.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=120"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/blog.xrhoujie.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=120"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}