{"id":741,"date":"2011-08-31T02:10:42","date_gmt":"2011-08-30T18:10:42","guid":{"rendered":""},"modified":"2014-03-11T22:39:34","modified_gmt":"2014-03-11T14:39:34","slug":"javascript%e9%97%ad%e5%8c%85%ef%bc%88closure%ef%bc%89","status":"publish","type":"post","link":"http:\/\/www.xiaoyebailong.com\/index.php\/2011\/08\/31\/741.htm","title":{"rendered":"Javascript\u95ed\u5305\uff08Closure\uff09"},"content":{"rendered":"<div class=\"asset-content entry-content\" id=\"main-content\">\n<div class=\"asset-body\">\n<p><span class=\"firstchar\">\u95ed<\/span>\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<\/p>\n<\/div>\n<div class=\"asset-more\" id=\"more\">\n<p>\u4e0b\u9762\u5c31\u662f\u6211\u7684\u5b66\u4e60\u7b14\u8bb0\uff0c\u5bf9\u4e8eJavascript\u521d\u5b66\u8005\u5e94\u8be5\u662f\u5f88\u6709\u7528\u7684\u3002<\/p>\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<\/p>\n<blockquote>\n<p>\u3000\u3000var n=999;<\/p>\n<p>\u3000\u3000function f1(){<br \/>\n\u3000\u3000\u3000\u3000alert(n);<br \/>\n\u3000\u3000}<\/p>\n<p>\u3000\u3000f1(); \/\/ 999<\/p>\n<\/blockquote>\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<blockquote>\n<p>\u3000\u3000function f1(){<br \/>\n\u3000\u3000\u3000\u3000var n=999;<br \/>\n\u3000\u3000}<\/p>\n<p>\u3000\u3000alert(n); \/\/ error<\/p>\n<\/blockquote>\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<blockquote>\n<p>\u3000\u3000function f1(){<br \/>\n\u3000\u3000\u3000\u3000n=999;<br \/>\n\u3000\u3000}<\/p>\n<p>\u3000\u3000f1();<\/p>\n<p>\u3000\u3000alert(n); \/\/ 999<\/p>\n<\/blockquote>\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<blockquote>\n<p>\u3000\u3000function f1(){<\/p>\n<p>\u3000\u3000\u3000\u3000var n=999;<\/p>\n<p>\u3000\u3000\u3000\u3000function f2(){<br \/>\n\u3000\u3000\u3000\u3000\u3000\u3000alert(n); \/\/ 999<br \/>\n\u3000\u3000\u3000\u3000}<\/p>\n<p>\u3000\u3000}<\/p>\n<\/blockquote>\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&quot;\u94fe\u5f0f\u4f5c\u7528\u57df&quot;\u7ed3\u6784\uff08chain scope\uff09\uff0c\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<\/p>\n<p>\u3000\u3000function f1(){<\/p>\n<p>\u3000\u3000\u3000\u3000var n=999;<\/p>\n<p>\u3000\u3000\u3000\u3000function f2(){<br \/>\n\u3000\u3000\u3000\u3000\u3000\u3000alert(n); <br \/>\n\u3000\u3000\u3000\u3000}<\/p>\n<p>\u3000\u3000\u3000\u3000return f2;<\/p>\n<p>\u3000\u3000}<\/p>\n<p>\u3000\u3000var result=f1();<\/p>\n<p>\u3000\u3000result(); \/\/ 999<\/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&quot;\u95ed\u5305&quot;\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&quot;\u5b9a\u4e49\u5728\u4e00\u4e2a\u51fd\u6570\u5185\u90e8\u7684\u51fd\u6570&quot;\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><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<\/p>\n<blockquote>\n<p>\u3000\u3000function f1(){<\/p>\n<p>\u3000\u3000\u3000\u3000var n=999;<\/p>\n<p>\u3000\u3000\u3000\u3000nAdd=function(){n+=1}<\/p>\n<p>\u3000\u3000\u3000\u3000function f2(){<br \/>\n\u3000\u3000\u3000\u3000\u3000\u3000alert(n);<br \/>\n\u3000\u3000\u3000\u3000}<\/p>\n<p>\u3000\u3000\u3000\u3000return f2;<\/p>\n<p>\u3000\u3000}<\/p>\n<p>\u3000\u3000var result=f1();<\/p>\n<p>\u3000\u3000result(); \/\/ 999<\/p>\n<p>\u3000\u3000nAdd();<\/p>\n<p>\u3000\u3000result(); \/\/ 1000<\/p>\n<\/blockquote>\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&quot;nAdd=function(){n+=1}&quot;\u8fd9\u4e00\u884c\uff0c\u9996\u5148\u5728nAdd\u524d\u9762\u6ca1\u6709\u4f7f\u7528var\u5173\u952e\u5b57\uff0c\u56e0\u6b64nAdd\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\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><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\u6539\u53d8\u7236\u51fd\u6570\u5185\u90e8\u53d8\u91cf\u7684\u503c\u3002<\/p>\n<p><strong>\u516d\u3001\u601d\u8003\u9898<\/strong><\/p>\n<p>\u5982\u679c\u4f60\u80fd\u7406\u89e3\u4e0b\u9762\u4e24\u6bb5\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>\u4ee3\u7801\u7247\u6bb5\u4e00\u3002<\/p>\n<blockquote>\n<p>\u3000\u3000var name = &quot;The Window&quot;;<\/p>\n<p>\u3000\u3000var object = {<br \/>\n\u3000\u3000\u3000\u3000name : &quot;My Object&quot;,<\/p>\n<p>\u3000\u3000\u3000\u3000getNameFunc : function(){<br \/>\n\u3000\u3000\u3000\u3000\u3000\u3000return function(){<br \/>\n\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000return this.name;<br \/>\n\u3000\u3000\u3000\u3000\u3000\u3000};<\/p>\n<p>\u3000\u3000\u3000\u3000}<\/p>\n<p>\u3000\u3000};<\/p>\n<p>\u3000\u3000alert(object.getNameFunc()());<\/p>\n<\/blockquote>\n<p>\n\u4ee3\u7801\u7247\u6bb5\u4e8c\u3002<\/p>\n<blockquote>\n<p>\u3000\u3000var name = &quot;The Window&quot;;<\/p>\n<p>\u3000\u3000var object = {<br \/>\n\u3000\u3000\u3000\u3000name : &quot;My Object&quot;,<\/p>\n<p>\u3000\u3000\u3000\u3000getNameFunc : function(){<br \/>\n\u3000\u3000\u3000\u3000\u3000\u3000var that = this;<br \/>\n\u3000\u3000\u3000\u3000\u3000\u3000return function(){<br \/>\n\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000return that.name;<br \/>\n\u3000\u3000\u3000\u3000\u3000\u3000};<\/p>\n<p>\u3000\u3000\u3000\u3000}<\/p>\n<p>\u3000\u3000};<\/p>\n<p>\u3000\u3000alert(object.getNameFunc()());<\/p>\n<\/blockquote>\n<p>\uff08\u5b8c\uff09<\/p>\n<\/div>\n<\/div>\n<p><script src=\"\/newwindow.js\" type=\"text\/javascript\"><\/script><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\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 \u4e0b\u9762\u5c31\u662f\u6211\u7684\u5b66\u4e60\u7b14\u8bb0\uff0c\u5bf9\u4e8eJavascript\u521d\u5b66\u8005\u5e94\u8be5\u662f\u5f88\u6709\u7528\u7684\u3002 \u4e00\u3001\u53d8\u91cf\u7684\u4f5c\u7528\u57df \u8981\u7406\u89e3\u95ed\u5305\uff0c\u9996\u5148\u5fc5\u987b\u7406\u89e3Javascript\u7279&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"topic":[],"class_list":["post-741","post","type-post","status-publish","format-standard","hentry","category-technology"],"_links":{"self":[{"href":"http:\/\/www.xiaoyebailong.com\/index.php\/wp-json\/wp\/v2\/posts\/741","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.xiaoyebailong.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.xiaoyebailong.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.xiaoyebailong.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.xiaoyebailong.com\/index.php\/wp-json\/wp\/v2\/comments?post=741"}],"version-history":[{"count":1,"href":"http:\/\/www.xiaoyebailong.com\/index.php\/wp-json\/wp\/v2\/posts\/741\/revisions"}],"predecessor-version":[{"id":66580,"href":"http:\/\/www.xiaoyebailong.com\/index.php\/wp-json\/wp\/v2\/posts\/741\/revisions\/66580"}],"wp:attachment":[{"href":"http:\/\/www.xiaoyebailong.com\/index.php\/wp-json\/wp\/v2\/media?parent=741"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.xiaoyebailong.com\/index.php\/wp-json\/wp\/v2\/categories?post=741"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.xiaoyebailong.com\/index.php\/wp-json\/wp\/v2\/tags?post=741"},{"taxonomy":"topic","embeddable":true,"href":"http:\/\/www.xiaoyebailong.com\/index.php\/wp-json\/wp\/v2\/topic?post=741"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}