{"id":3090,"date":"2014-10-02T00:37:01","date_gmt":"2014-10-01T16:37:01","guid":{"rendered":"https:\/\/sdeno.com\/?p=3090"},"modified":"2020-11-07T23:33:59","modified_gmt":"2020-11-07T15:33:59","slug":"js%e9%97%ad%e5%8c%85%e7%ae%80%e5%8d%95%e4%ba%86%e8%a7%a3","status":"publish","type":"post","link":"https:\/\/sdeno.com\/?p=3090","title":{"rendered":"js\u95ed\u5305\u7b80\u5355\u4e86\u89e3"},"content":{"rendered":"<p>1\uff0c\u51fd\u6570\u4f5c\u4e3a\u8fd4\u56de\u503c<\/p>\n<pre>var a=20\r\n\r\nfunction haha() {\r\n  console.log('haha1')\r\n}\r\n\r\n\r\nfunction wo() {\r\n  \/\/ var a=10\r\n    function haha() {\r\n        console.log('haha2')\r\n    }\r\n    return function () { \/\/\u533f\u540d\u51fd\u6570\u662f\u5728wo\u51fd\u6570\u91cc\u9762\u58f0\u660e\u7684\uff0c\u5c31\u5148\u5728wo\u51fd\u6570\u91cc\u9762\u627e\u9700\u8981\u7684\u53d8\u91cf\r\n         console.log(a)\r\n         haha()\r\n    }\r\n}\r\n\r\nvar fn=wo()\r\nfn() \/\/\u867d\u7136\u5728\u5168\u5c40\u73af\u5883\u8c03\u7528\u4e86\u8fd4\u56de\u7684\u533f\u540d\u51fd\u6570\uff0c\u4f46\u662f\u533f\u540d\u51fd\u6570\u4f1a\u53bb\u5f80\u58f0\u660e\u5b83\u7684\u73af\u5883\u91cc\u9762\u5411\u4e0a\u5bfb\u627e<\/pre>\n<p>&nbsp;<\/p>\n<p>2\uff0c\u51fd\u6570\u4f5c\u4e3a\u53c2\u6570\u65f6<\/p>\n<pre>var a=20\r\nfunction wo(fn) {\r\n     var a=10\r\n     fn() \/\/ni\u51fd\u6570\u867d\u7136\u662f\u5728wo\u51fd\u6570\u91cc\u9762\u88ab\u8c03\u7528\uff0c\u4f46\u662fni\u51fd\u6570\u662f\u5728\u5168\u5c40\u73af\u5883\u4e2d\u88ab\u58f0\u660e\u7684\uff0c\u6240\u4ee5\u5148\u53bb\u5168\u5c40\u73af\u5883\u627e\r\n}\r\n\r\nfunction ni() {\r\n     console.log(a)\r\n}\r\n\r\nwo(ni)<\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-\u5148\u770b\u4ee5\u4e0a\u6848\u4f8b&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-<\/p>\n<p><span style=\"color: #ff0000;\">1\uff0c\u65e2\u80fd\u8bbf\u95ee\u5168\u5c40\u53c8\u80fd\u8bbf\u95ee\u67d0\u4e00\u4e2a\u51fd\u6570\u7684\u4f5c\u7528\u57df<\/span>\uff0c\u4ee5\u4e0b\u662f\u6848\u4f8b\uff1a<br \/>\n<a href=\"https:\/\/sdeno.com\/wp-content\/uploads\/2014\/10\/bibao1.png\"><figure class=\"mdx-lazyload-container\" style=\"max-width:861px\"><div style=\"padding-top:30.662020905923%\"><\/div><div class=\"mdx-img-loading-sp mdui-valign\"><div><div class=\"mdui-spinner\"><\/div><\/div><\/div><img width=\"861\" height=\"264\" class=\"alignnone size-full wp-image-4793 lazyload\" title=\"bibao1\" src=\"data:image\/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==\" data-src=\"https:\/\/sdeno.com\/wp-content\/uploads\/2014\/10\/bibao1.png\" alt=\"bibao1\" data-srcset=\"https:\/\/sdeno.com\/wp-content\/uploads\/2014\/10\/bibao1.png 861w, https:\/\/sdeno.com\/wp-content\/uploads\/2014\/10\/bibao1-300x91.png 300w\" sizes=\"auto, (max-width: 861px) 100vw, 861px\"><\/figure><\/a><\/p>\n<p>&nbsp;<\/p>\n<p>\u9996\u5148\u7b80\u5355\u770b\u4e00\u4e0b\u4e00\u4e2a\u7b80\u5355\u7684\u95ed\u5305<\/p>\n<p>function box(){<\/p>\n<p>alert(&#8216;\u533f\u540d\u51fd\u6570\u5916&#8217;);<br \/>\nreturn function(){ return &#8216;fff&#8217;; };<br \/>\n};<\/p>\n<p>\u8c03\u7528\u65b9\u6cd5\u4e00\uff1aalert(box()());<br \/>\n\u8c03\u7528\u65b9\u6cd5\u4e8c\uff1a<br \/>\nvar b=box();\/\/\u5f53\u6211\u4eec\u628abox\u51fd\u6570\u8d4b\u503c\u7ed9\u4e86\u53d8\u91cfb\u65f6\uff0c\u5c31\u5df2\u7ecf\u6267\u884c\u4e86\u51fd\u6570box,\u4f46\u662f\u6ca1\u6267\u884c\u5230\u533f\u540d\u51fd\u6570\u800c\u5df2\u3002<br \/>\nalert(b()); \/\/\u8fd9\u4e00\u6b65\u5176\u5b9e\u5c31\u53ea\u8c03\u7528\u4e86function(){ return &#8216;fff&#8217;; };<\/p>\n<p>\u4e00\u4e2a\u51fd\u6570\u91cc\u9762\u6709\u533f\u540d\u51fd\u6570\u3002<br \/>\n<span style=\"color: #ff0000;\">2\uff0c\u95ed\u5305\u7684\u4f5c\u7528\u5c31\u662f\u907f\u514d\u58f0\u660e\u4e00\u4e2a\u5168\u5c40\u53d8\u91cf<\/span>\uff0c\u6d88\u8017\u8d44\u6e90\u6216\u8005\u51fa\u73b0\u91cd\u590d\u91cd\u547d\u540d\u5bfc\u81f4\u4e0d\u53ef\u9884\u6d4b\u7ed3\u679c\uff0c\u5176\u5b9e\u6548\u679c\u5c31\u662f<br \/>\n\u4e00\u4e2a\uff1a\u4e0d\u58f0\u660e\u5168\u5c40\u53d8\u91cf\u7684\u60c5\u51b5\u4e0b\uff0c\u62e5\u6709\u548c\u5168\u5c40\u53d8\u91cf\u4e00\u6837\u7684\u6548\u679c\uff0c\u800c\u4e14\u6682\u65f6\u4f4f\u5b85\u5185\u5b58\uff0c\u4e0d\u7528\u7684\u65f6\u5019\u53ef\u4ee5\u91ca\u653e\u51fa\u53bb\uff0c<br \/>\n\u8fd9\u6837\u5c31\u53ef\u4ee5\u8282\u7701\u8d44\u6e90\u3002<\/p>\n<p>&nbsp;<\/p>\n<p>\u4e00\u4e2a\u7b80\u5355\u4f8b\u5b50\u6765\u8bf4\u660e\u95ed\u5305\u7684\u4f5c\u7528\u3002<\/p>\n<pre>var age=100; \/\/\u5168\u5c40\u53d8\u91cfage\r\n\r\nfunction box(){\r\n  age++; \/\/\u7d2f\u52a0\u5168\u5c40\u53d8\u91cf\r\n  return age;\r\n};\r\nalert(box());\r\nalert(box());\r\nalert(box());\r\n<\/pre>\n<p>\u5168\u5c40\u53d8\u91cf\u597d\u7528\u4f46\u662f\u6211\u4eec\u505a\u9879\u76ee\u7684\u65f6\u5019\u80fd\u907f\u514d\u4f7f\u7528\u5168\u5c40\u5c31\u5c3d\u91cf\u907f\u514d<br \/>\n\u95ed\u5305\u4f8b\u5b50\uff1a<\/p>\n<pre>function box(){\r\n  var age=100; \/\/\u5916\u90e8\u76f4\u63a5\u662f\u4e0d\u80fd\u65b9\u9762\u7684\r\n  alert('var age\u88ab\u6267\u884c\u4e86\u4e00\u6b21\u800c\u5df2');\r\n  return function(){\r\n    age++;\r\n    alert('\u533f\u540d\u51fd\u6570\u4e0d\u65ad\u7684\u88ab\u6267\u884c');\r\n    return age;\r\n  }\r\n}\r\nalert(box()());\r\nalert(box()());\r\nalert(box()()); \/\/\u8fd9\u6837\u8c03\u7528\u662f\u4e0d\u80fd\u5b9e\u73b0\u7d2f\u52a0\u7684\uff0c\u56e0\u4e3a\u8fd9\u6837\u8c03\u7528\u4e5f\u4e0d\u65ad\u7684\u6267\u884c\u5230\u4e86var age=100;age\u4e5f\u5c31\u4e0d\u65ad\u7684\u88ab\u521d\u59cb\u5316\u4e86\u3002\r\n\r\n\u6211\u4eec\u8981\u8fd9\u6837\u8c03\u7528\uff1a\r\nvar b = box();\/\/\u6267\u884c\u5230\u4e86var age=100; \u4e5f\u5c31\u662f\u5728box\u51fd\u6570\u4e2d\u5185\u90e8\u4e2d\u58f0\u660e\u4e86age=100\uff0c\u4f46\u662f\u6ca1\u6267\u884c\u533f\u540d\u51fd\u6570\u3002\r\nalert(b()); \/\/\u7b2c\u4e00\u6b21\u8c03\u7528\u65f6\uff0c\u6267\u884c\u4e86box\u51fd\u6570\u91cc\u9762\u7684\u5168\u90e8\u4ee3\u7801\u3002\r\nalert(b()); \/\/\u7b2c\u4e8c\u6b21\u8c03\u7528\u65f6\uff0c\u5176\u5b9e\u5c31\u53ea\u6267\u884c\u4e86\u533f\u540d\u51fd\u6570\u90a3\u90e8\u5206\u800c\u5df2\uff0c\u6ca1\u6709\u8df3\u51fabox\u51fd\u6570\u5916\u3002\r\nalert(b()); \/\/\u6ca1\u8df3\u51fabox\u51fd\u6570\uff0c\u5c31\u53ea\u6267\u884c\u4e86\u533f\u540d\u51fd\u6570\u90e8\u5206\u3002\r\nb=null; \/\/\u8fd9\u6837\u5c31\u6ce8\u9500\u4e86age\u5728\u5185\u5b58\u91cc\u7684\u4f4f\u5b85\u3002\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-<\/p>\n<p>\u95ed\u5305\u7684 \u6570\u7ec4\u5faa\u73af\u53d6\u503c\u95ee\u9898\u7684\u5e94\u7528\u6ce8\u610f\u4e8b\u9879<br \/>\n\/\/\u65b9\u6cd51<br \/>\nfunction box(){<br \/>\nvar wo=[];<br \/>\nfor(var i=0;i&lt;5;i++){<br \/>\nwo[i]=(function(mun){ return mun; })(i);<br \/>\n}<\/p>\n<p>return wo;<br \/>\n}<\/p>\n<p>alert( box()[0] );<br \/>\nalert( box()[1] );<br \/>\n\/\/\u65b9\u6cd52<\/p>\n<p>function box(){<br \/>\nvar wo=[];<br \/>\nfor(var i=0;i&lt;5;i++){<br \/>\nwo[i]=(function(mun){<br \/>\nreturn function(){<br \/>\nreturn mun;<br \/>\n}<br \/>\n})(i);<br \/>\n}<\/p>\n<p>return wo;<br \/>\n}<br \/>\nalert( box()[0]() );<br \/>\nalert( box()[1]() );<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>\u95ed\u5305\u4e2d\u7684this\u6307\u5411\u95ee\u9898\u3002<\/p>\n<p>var box={<br \/>\nrun:function(){<br \/>\nreturn function(){<br \/>\nreturn this; \/\/\u8fd9\u4e2athis\u53ea\u5f97\u662fwindow<br \/>\n}<br \/>\n}<br \/>\n}<\/p>\n<p>*\u95ed\u5305\u5176\u5b9e\u6a21\u62df\u4e86\u5168\u5c40\u7684\u73af\u5883\u95ed\u5305\u91cc\u9762\u7684\u53d8\u91cf\u53ea\u6240\u4ee5\u53ef\u4ee5\u6682\u65f6\u4f4f\u5b85\u5c31\u662f\u56e0\u4e3a\u95ed\u5305\u73af\u5883\u6a21\u62df\u4e86\u5168\u5c40\uff0c\u6240\u4ee5\u95ed\u5305\u91cc\u9762\u7684this\u624d\u80fd\u6307\u5411window<br \/>\n\u90a3\u4e48\u5982\u4f55\u5c06\u95ed\u5305\u91cc\u9762\u7684this\u6307\u5411\u4f5c\u7528\u57df\u7684\u5bf9\u8c61\u5462\uff1f<br \/>\n\u65b9\u6cd5\u4e00\uff1a\u5bf9\u8c61\u5192\u5145<\/p>\n<p>box.run().call(box) \/\/\u8ba9\u95ed\u5305\u5192\u5145box<br \/>\n\u65b9\u6cd5\u4e8c\uff1a<br \/>\nvar box={<br \/>\nrun:function(){<br \/>\nvar that=this; \/\/\u628abox\u7684this\u8d4b\u503c\u7ed9\u4e00\u4e2a\u53d8\u91cf<br \/>\nreturn function(){<br \/>\nreturn that; \/\/\u5728\u95ed\u5305\u4e2d\u4f7f\u7528box\u7684this\u53d8\u91cf<br \/>\n}<br \/>\n}<br \/>\n}<br \/>\n\u79c1\u6709\u4f5c\u7528\u57df\uff0c\u4e3e\u4e24\u4e2a\u4f8b\u5b50\u3002<br \/>\n\u4f8b\u5b50\u4e00\uff1a<br \/>\nfunction box(){<br \/>\nfor(var i=0;i&lt;5;i++){ \/\/\u5728JS\u4e2dfor\u5b9a\u4e49\u7684i\u5728\u5faa\u73af\u4f53\u91cc\u5229\u7528\u5b8c\u4e86\uff0ci\u5728\u5faa\u73af\u4f53\u5916\u4ecd\u7136\u53ef\u4ee5\u7528\u3002<br \/>\nalert(i);<br \/>\n}<\/p>\n<p>alert(i); \/\/\u8fd9\u91cc\u7684i\u662f\u5faa\u73af\u4f53\u4e2d\u7684i\uff0c\u5728\u5faa\u73af\u4f53\u5916\u9762\u8fd8\u53ef\u4ee5\u7528\u3002<br \/>\n}<br \/>\nbox();<\/p>\n<p>&nbsp;<\/p>\n<p>\u6211\u4eec\u4e0d\u60f3\u8ba9\u5faa\u73af\u4f53\u91cc\u9762\u7684i\u5728\u5916\u9762\u4e5f\u6709\u6548\u53ef\u4ee5\u8fd9\u4e48\u505a\uff1a<\/p>\n<p>function box(){<br \/>\n(function(){<br \/>\nfor(var i=0;i&lt;5;i++){<br \/>\nalert(i);<br \/>\n}<br \/>\n})(); \/\/\u8df3\u51fa\u81ea\u8c03\u7528\u5faa\u73af\u4f53\u7684i\u5df2\u7ecf\u88ab\u6ce8\u9500\u4e86<\/p>\n<p>alert(i); \/\/\u8fd9\u91cc\u7684i\u548c\u5faa\u73af\u4f53\u7684i\u6ca1\u6709\u5173\u7cfb<br \/>\n}<br \/>\n\u4f8b\u5b50\u4e8c\uff1a<br \/>\n\u5728\u9879\u76ee\u4e2d\u90fd\u662f\u5f88\u591a\u7a0b\u5e8f\u5458\u505a\u7684\uff0c\u4e0d\u53ef\u80fd\u6bcf\u4e2a\u4eba\u90fd\u58f0\u660e\u53d8\u91cf\u5728\u5168\u5c40\u4e2d\uff0c\u8fd9\u6837\u4e5f\u8bb8\u4f1a\u6709\u4eba\u547d\u540d\u91cd\u590d\u7684\uff0c\u540c\u65f6<br \/>\n\u547d\u540d\u591a\u4e86\u4e5f\u5360\u8d44\u6e90\uff0c\u6240\u4ee5\u6211\u4eec\u9700\u8981\u81ea\u8c03\u7528\u51fd\u6570\u7684\u65b9\u6cd5\u5192\u5145\u58f0\u660e\u5168\u5c40\u53d8\u91cf\uff0c\u51fd\u6570\u6267\u884c\u5b8c\u8df3\u51fa\u4e4b\u540e\uff0c\u53d8\u91cf\u4e5f<br \/>\n\u4f1a\u81ea\u52a8\u9500\u6bc1\u3002<\/p>\n<p>;(function(){<br \/>\nvar age=100; \/\/\u8fd9\u91cc\u58f0\u660e\u7684\u5168\u5c40\u53d8\u91cf\u6682\u65f6\u5b58\u5728\u5185\u5b58\u4e2d\u3002<br \/>\n})();<\/p>\n<p>\/\/\u5230\u4e86\u51fd\u6570\u5916\u9762age\u8ddf\u5df2\u7ecf\u4e0d\u5b58\u5728\u4e86\u3002<\/p>\n<p>alert(age); \/\/\u8fd9\u91cc\u7684age\u8ddf\u81ea\u8c03\u7528\u51fd\u6570\u91cc\u9762\u7684age\u6ca1\u5173\u7cfb\u3002<\/p>\n<p>&nbsp;<\/p>\n<p>\u6240\u4ee5\u5728\u9879\u76ee\u5f00\u53d1\u4e2d\uff0c\u79c1\u6709\u4f5c\u7528\u57df\u7684\u4f5c\u7528\u5f88\u91cd\u8981\u3002<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>\u79c1\u6709\u53d8\u91cf<\/p>\n<p>var box=function(value){<br \/>\nvar wo=value; \/\/\u5916\u90e8\u76f4\u63a5\u662f\u65e0\u6cd5\u8c03\u7528\u7684<br \/>\nthis.run=function(){ \/\/\u53ea\u80fd\u5728\u5185\u90e8\u63d0\u4f9b\u63a5\u53e3\uff0c\u5916\u90e8\u624d\u80fd\u8bbf\u95ee<br \/>\nreturn wo;<br \/>\n}<br \/>\n}<\/p>\n<p>var p1=new box(&#8216;\u9648&#8217;);<br \/>\nalert(p1.run());<br \/>\n\u5728\u79c1\u6709\u4f5c\u7528\u57df\u4e2d\u8fbe\u5230\u6784\u9020\u51fd\u6570\u65b9\u6cd5\u5171\u4eab<\/p>\n<p>\u6211\u4eec\u5e73\u65f6\u5e73\u65f6\u90fd\u662f\u8fd9\u6837\u5199<br \/>\nvar box=function(value){<br \/>\nvar wo=value;<\/p>\n<p>box.prototype.run=function(){<br \/>\nreturn wo;<br \/>\n}<br \/>\n}<br \/>\nvar p1=new box(&#8216;\u9648&#8217;);<br \/>\nalert(p1.run());<\/p>\n<p>var p2=new box(&#8216;fff&#8217;); \/\/p2\u8986\u76d6\u4e86\u4fee\u6539\u4e86\u539f\u578b\u51fd\u6570value\u503c<br \/>\nalert(p1.run()); \/\/\u5373\u4f7f\u662f\u5f39\u51fa\u4e86p1.run()\u4e5f\u80fd\u5f97\u5230p2\u4fee\u6539\u7684\u503c\u3002<\/p>\n<p>\u4ee5\u4e0a\u662f\u6211\u4eec\u5e73\u65f6\u7684\u5199\u6cd5\u3002<\/p>\n<p>\u5982\u679c\u8981\u5728\u79c1\u7528\u7684\u4f5c\u7528\u57df\u60c5\u51b5\u4e0b\u521b\u5efa\u5b9e\u73b0\u4ee5\u4e0a\u6548\u679c\u6211\u4eec\u8981\u8fd9\u6837\u505a\uff1a<br \/>\n\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u9759\u6001\u79c1\u6709\u53d8\u91cf\u4e5f\u80fd\u8fbe\u5230\u4ee5\u4e0a\u6548\u679c<\/p>\n<p>(function(){<br \/>\nvar wo=&#8221;; \/\/\u79c1\u7528<br \/>\nbox=function(value){ \/\/\u4e0d\u52a0var\u521b\u5efa\u5168\u5c40\u6784\u9020\u51fd\u6570\uff0c\u8fd9\u6837\u5916\u9762\u624d\u53ef\u4ee5\u5b9e\u4f8b\u5316\u6b64\u6784\u9020\u51fd\u6570<br \/>\nwo=value;<br \/>\n}<br \/>\nbox.prototype.run=function(){<br \/>\nreturn wo;<br \/>\n}<\/p>\n<p>box.prototype.setrun=function(value){<br \/>\nreturn wo=value;<br \/>\n}<\/p>\n<p>})();<br \/>\nvar p1 = new box(&#8216;\u6211&#8217;);<br \/>\nalert(p1.run());<\/p>\n<p>var p2 = new box(&#8216;\u4f60&#8217;);<br \/>\nalert(p1.run());<\/p>\n<p>var p3 = new box();<br \/>\nalert(p1.setrun(&#8216;\u4ed6&#8217;));<\/p>\n<p><a href=\"https:\/\/sdeno.com\/wp-content\/uploads\/2014\/10\/jsbb.txt\">jsbb<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>1\uff0c\u51fd\u6570\u4f5c\u4e3a\u8fd4\u56de\u503c var a=20 function haha() { console.log(&#8216;haha1 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[26,11],"tags":[],"class_list":["post-3090","post","type-post","status-publish","format-standard","hentry","category-javascript","category-11"],"_links":{"self":[{"href":"https:\/\/sdeno.com\/index.php?rest_route=\/wp\/v2\/posts\/3090","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sdeno.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sdeno.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sdeno.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/sdeno.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3090"}],"version-history":[{"count":0,"href":"https:\/\/sdeno.com\/index.php?rest_route=\/wp\/v2\/posts\/3090\/revisions"}],"wp:attachment":[{"href":"https:\/\/sdeno.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3090"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sdeno.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3090"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sdeno.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3090"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}