{"id":116,"date":"2017-02-02T14:21:16","date_gmt":"2017-02-02T06:21:16","guid":{"rendered":"http:\/\/blog.xrhoujie.com\/?p=116"},"modified":"2017-10-11T10:33:29","modified_gmt":"2017-10-11T02:33:29","slug":"javascript%e9%97%ad%e5%8c%85%e7%90%86%e8%a7%a32","status":"publish","type":"post","link":"http:\/\/blog.xrhoujie.com\/?p=116","title":{"rendered":"(\u8f6c)JavaScript\u95ed\u5305\u7406\u89e3(2)"},"content":{"rendered":"<div id=\"blogBody\" class=\"blog-body\">\n<div class=\"BlogContent\">\n<p>\u95ed\u5305\u662fjavascript\u4e2d\u7ed5\u4e0d\u5f00\u7684\u8bdd\u9898\uff0c\u5173\u4e8e\u95ed\u5305\u7684\u4e00\u4e9b\u6982\u5ff5\u548c\u5e94\u7528\uff0c\u8fd9\u65b9\u9762\u8d44\u6599\u6bd4\u8f83\u591a\uff0c\u5728\u6b64\u5c31\u4e0d\u518d\u8d58\u8ff0\u3002\u4f17\u6240\u5468\u77e5\uff0c\u95ed\u5305\u7684\u4e00\u4e2a\u4f5c\u7528\u5c31\u662f \u8ba9\u4e00\u4e9b\u53d8\u91cf\u59cb\u7ec8\u4fdd\u6301\u5728\u5185\u5b58\u4e2d \uff0c\u5728\u6b64\u6211\u7528\u4e00\u4e9b\u5b9e\u9645\u4ee3\u7801\u5bf9\u8fd9\u53e5\u8bdd\u4f5c\u8fdb\u4e00\u6b65\u7684\u7406\u89e3\u3002<\/p>\n<pre class=\"hljs javascript\"><code class=\"language-javascript hljs\">\r\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">closure<\/span>()<\/span>{\r\n  <span class=\"hljs-keyword\">var<\/span> n = <span class=\"hljs-number\">1<\/span>;\r\n  <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">test<\/span>() <\/span>{\r\n    <span class=\"hljs-built_in\">console<\/span>.log(++n);\r\n  }\r\n  test() \r\n  test() \r\n}\r\nn(); <span class=\"hljs-comment\">\/\/\u8f93\u51fa2,3\uff08\u8fd9\u4e2a\u5f88\u597d\u7406\u89e3\uff0c\u4e0d\u591a\u8bf4\uff09<\/span>\r\n\r\n\r\n<\/code><\/pre>\n<pre class=\"hljs javascript\"><code class=\"hljs javascript\"><span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">test<\/span>()<\/span>{\r\n    <span class=\"hljs-keyword\">var<\/span> i=<span class=\"hljs-number\">1<\/span>;\r\n    <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span>()<\/span>{\r\n        <span class=\"hljs-built_in\">console<\/span>.log(i++);\r\n    }\r\n    }\r\n    <span class=\"hljs-keyword\">var<\/span> fn = test();\r\n    fn(); <span class=\"hljs-comment\">\/\/\u8f93\u51fa1<\/span>\r\n    fn(); <span class=\"hljs-comment\">\/\/\u8f93\u51fa2<\/span><\/code><\/pre>\n<p>\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0cfn\u5c31\u662f\u6267\u884c\u51fd\u6570test()\u8fd4\u56de\u7684\u533f\u540d\u51fd\u6570\u3002\u7531\u4e8efn\u53c8\u662f\u4e00\u4e2a\u5168\u5c40\u53d8\u91cf\uff0c\u751f\u547d\u5468\u671f\u4e00\u76f4\u5b58\u5728\uff0c\u4e14\u8fd9\u4e2a\u533f\u540d\u51fd\u6570\u5f15\u7528\u4e86\u7236\u51fd\u6570test\u91cc\u9762\u7684\u4e00\u4e2a\u5c40\u90e8\u53d8\u91cf\uff0c\u6240\u4ee5\u53d8\u91cfi\u7684\u72b6\u6001\u4f1a\u88ab\u4e00\u76f4\u4fdd\u5b58.<\/p>\n<p>3<\/p>\n<pre class=\"hljs javascript\"><code class=\"hljs javascript\"><span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">test<\/span>()<\/span>{\r\n    <span class=\"hljs-keyword\">var<\/span> i=<span class=\"hljs-number\">0<\/span>;\r\n    <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span>()<\/span>{\r\n        <span class=\"hljs-built_in\">console<\/span>.log(i++);\r\n    }\r\n}\r\ntest()(); <span class=\"hljs-comment\">\/\/\u8f93\u51fa0<\/span>\r\ntest()(); <span class=\"hljs-comment\">\/\/\u8f93\u51fa0<\/span><\/code><\/pre>\n<p>\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6267\u884ctest()\u8fd4\u56de\u7684\u662f\u5176\u5185\u90e8\u7684\u533f\u540d\u51fd\u6570\uff0c\u4f46\u662f\u6267\u884c\u5b8ctest()()\u4ee5\u540e\uff0c\u8be5\u533f\u540d\u51fd\u6570\u7684\u751f\u547d\u5468\u671f\u5df2\u7ecf\u7ed3\u675f\uff0c\u6240\u4ee5\u53d8\u91cfi\u7684\u72b6\u6001\u6ca1\u6709\u5f97\u5230\u4fdd\u5b58\u3002<\/p>\n<p>\u6700\u8fd1\u5728\u7b80\u4e66\u4e0a\u8bfb\u5230\u4e00\u7bc7\u6709\u8da3\u7684\u6587\u7ae0\uff0c\u5176\u4e2d\u4e00\u4e9b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre class=\"hljs lisp\"><code class=\"hljs lisp\">for (<span class=\"hljs-name\">var<\/span> i = <span class=\"hljs-number\">0<\/span><span class=\"hljs-comment\">; i &lt; 5; i++) {<\/span>\r\n setTimeout((<span class=\"hljs-name\">function<\/span>(<span class=\"hljs-name\">i<\/span>) {\r\n    console.log(<span class=\"hljs-name\">i<\/span>)<span class=\"hljs-comment\">;<\/span>\r\n  })(<span class=\"hljs-name\">i<\/span>), i * 1000);\r\n}   \/\/\u8f93\u51fa0\uff0c1,2,3,4\r\n<\/code><\/pre>\n<p>\u4e0a\u8ff0\u4ee3\u7801\u4e2d\u6709\u4e00\u4e2asetTimeout()\u51fd\u6570\uff0c\u5176\u4f7f\u7528\u683c\u5f0f\u4e3asetTimeout(func,time),\u610f\u601d\u662f\u81f3\u5c11\u8fc7time\u65f6\u95f4\u540e\uff0c\u6267\u884cfunc\u3002\u6709\u4e86\u8fd9\u6837\u7684\u8ba4\u8bc6\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u628afunc\u7b49\u4ef7\u6210\uff1a<br \/>\n(function(i){console.log(i)})(i)\u8fd9\u4e2a\u81ea\u6267\u884c\u51fd\u6570\u3002\u6240\u4ee5\u8fd9\u4e2a\u4e8b\u60c5\u5206\u4e24\u6b65\uff0c\u9996\u5148\u6bcf\u6b21\u5faa\u73af\u81ea\u6267\u884c\u51fd\u6570\u80af\u5b9a\u4f1a\u7acb\u5373\u6267\u884c\u4e00\u6b21\uff0c\u4f1a\u4f9d\u6b21\u8f93\u51fa1,2,3,4\u3002\u5176\u6b21\uff0c\u6bcf\u8fc7time\u65f6\u95f4\uff0c\u6267\u884c\u4e00\u6b21func\uff0c\u4f46\u662f\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0cfunc\u7b49\u4ef7\u4e8e\u4e00\u4e2a\u7acb\u5373\u6267\u884c\u51fd\u6570\uff0c\u6ca1\u6709\u8fd4\u56de\u503c\uff0c\u6545\u4ec0\u4e48\u4e5f\u4e0d\u4f1a\u505a\u3002<br \/>\n\u5982\u679c\u628a\u4e0a\u8ff0\u4ee3\u7801\u6539\u6210\u8fd9\u6837\uff1a<\/p>\n<pre class=\"hljs lisp\"><code class=\"hljs lisp\">for (<span class=\"hljs-name\">var<\/span> i = <span class=\"hljs-number\">0<\/span><span class=\"hljs-comment\">; i &lt; 5; i++) {<\/span>\r\n  var j=7<span class=\"hljs-comment\">;<\/span>\r\n  setTimeout((<span class=\"hljs-name\">function<\/span>(<span class=\"hljs-name\">i<\/span>) {\r\n    console.log(<span class=\"hljs-name\">i<\/span>)<span class=\"hljs-comment\">;<\/span>\r\n    return function(){\r\n       console.info(<span class=\"hljs-name\">j++<\/span>)<span class=\"hljs-comment\">;<\/span>\r\n    }\r\n  })(<span class=\"hljs-name\">i<\/span>), i * 1000);\r\n}<\/code><\/pre>\n<p>\u4e0a\u8ff0\u4ee3\u7801\u4f1a\u4f9d\u6b21\u8f93\u51fa0\uff0c1,2,3,4\uff0c 7,8,9,10,11(\u7ed3\u5408\u8fd9\u4e2a\u4f8b\u5b50\u7406\u89e3\u4e0a\u4e00\u4e2a\u95ee\u9898\uff0c\u5c31\u5bb9\u6613\u591a\u4e86)<\/p>\n<p>\u5982\u679c\u628a\u4e0a\u9762\u7684\u4ee3\u7801\u6539\u6210\u8fd9\u6837\u5462\uff1f<\/p>\n<pre class=\"hljs javascript\"><code class=\"hljs javascript\"><span class=\"hljs-keyword\">for<\/span> (<span class=\"hljs-keyword\">var<\/span> i = <span class=\"hljs-number\">0<\/span>; i &lt; <span class=\"hljs-number\">5<\/span>; i++) {\r\n  setTimeout(<span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span>() <\/span>{\r\n    <span class=\"hljs-built_in\">console<\/span>.log(i);\r\n  }, i * <span class=\"hljs-number\">000<\/span>);\r\n}<\/code><\/pre>\n<p>\u8fd0\u884c\u4e0a\u9762\u7684\u4ee3\u7801\u4f1a\u8f93\u51fa5\u4e2a5\uff08\u8fd9\u4e00\u70b9\u73b0\u5728\u53ef\u4ee5\u7406\u89e3\u4e86\u5427\uff09\uff0c\u6709\u4eba\u4e0d\u89e3\u7684\u662f\u4e3a\u4ec0\u4e48\u662f\u6bcf\u9694\u4e00\u79d2\u8f93\u51fa5\u800c\u4e0d\u662f\u9694\u4e94\u79d2\u8f93\u51fa5\u3002setTimeout(func,time)\u8fd9\u4e2a\u51fd\u6570\u5ef6\u8fdf\u6267\u884c\u7684\u53ea\u662ffunc\u91cc\u9762\u7684\u52a8\u4f5c\uff0c\u800ctime\u7684\u503c\u662f\u4e0d\u5b58\u5728\u5ef6\u8fdf\u7684\u3002\u4e0a\u9762\u6267\u884cfor\u5faa\u73af\uff0c\u76f8\u5f53\u4e8e\uff1asetTimeout(func,1000)\uff0csetTimeout(func,2000) &#8230;. \u7b2c\u4e00\u6b21setTimeout\u6267\u884c\u7684\u65f6\u5019\u7b49\u4e86\u4e00\u79d2\uff08\u51c6\u786e\u5730\u8bf4\uff0c\u81f3\u5c11\u8981\u7b49\u4e00\u79d2\uff09\uff0c\u7b2c\u4e8c\u6b21setTimeout\u6267\u884c\u7684\u65f6\u5019\u7b492\u79d2\uff0c\u4f46\u662f\u524d\u9762\u7b2c\u4e00\u6b21\u6267\u884c\u7684setTimeout\u51fd\u6570\u65f6\uff0c\u5df2\u7ecf\u7b49\u4e86\u4e00\u79d2\uff0c\u6240\u4ee5\u53ea\u9700\u518d\u7b49\u4e00\u79d2\u5c31\u53ef\u4ee5\u7acb\u5373\u6267\u884c\u4e86\u3002\u5728\u8fd9\u91cc\uff0c\u9700\u8981\u5f04\u660e\u767d\u7684\u662f\uff0csetTimeout\u51fd\u6570\u5ef6\u8fdf\u6267\u884c\u7684\u65f6\u95f4\uff0c\u8d77\u70b9\u662f\u4ecetime\u5f00\u59cb\u8d4b\u503c\u7684\u65f6\u5019\u8ba1\u7b97\u7684\uff0c\u4e0d\u662f\u4ece\u4e0a\u4e00\u4e2asetTimeout\u51fd\u6570\u6267\u884c\u5b8c\u5f00\u59cb\u7b97\u7684\u3002<\/p>\n<\/div>\n<\/div>\n<div class=\"blog-opr\">\n<div class=\"blog-copyright\">\u539f\u6587\u5730\u5740\uff1a<a href=\"https:\/\/segmentfault.com\/a\/1190000008571702\" target=\"_blank\" rel=\"noopener noreferrer\">https:\/\/segmentfault.com\/a\/1190000008571702<\/a><\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u95ed\u5305\u662fjavascript\u4e2d\u7ed5\u4e0d\u5f00\u7684\u8bdd\u9898\uff0c\u5173\u4e8e\u95ed\u5305\u7684\u4e00\u4e9b\u6982\u5ff5\u548c\u5e94\u7528\uff0c\u8fd9\u65b9\u9762\u8d44\u6599\u6bd4\u8f83\u591a\uff0c\u5728\u6b64\u5c31\u4e0d\u518d\u8d58\u8ff0\u3002\u4f17\u6240\u5468\u77e5 [&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\/116"}],"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=116"}],"version-history":[{"count":4,"href":"http:\/\/blog.xrhoujie.com\/index.php?rest_route=\/wp\/v2\/posts\/116\/revisions"}],"predecessor-version":[{"id":324,"href":"http:\/\/blog.xrhoujie.com\/index.php?rest_route=\/wp\/v2\/posts\/116\/revisions\/324"}],"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=116"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/blog.xrhoujie.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=116"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/blog.xrhoujie.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=116"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}