{"id":2045,"date":"2017-10-13T16:02:29","date_gmt":"2017-10-13T12:18:16","guid":{"rendered":"http:\/\/www.xttblog.com\/?p=2045"},"modified":"2017-10-13T16:04:46","modified_gmt":"2017-10-13T08:04:46","slug":"%e9%9d%a0%e6%8e%92%e7%85%a7%e7%89%87%e5%92%8c%e6%8b%8d%e8%a7%86%e9%a2%91%e5%81%9a%e5%88%b0%e6%9c%88%e5%85%a510%e4%b8%87-15","status":"publish","type":"post","link":"https:\/\/www.xttblog.com\/?p=2045","title":{"rendered":"js\u9012\u5f52\u51fd\u6570\u9020\u6210\u7684\u5185\u5b58\u6ea2\u51fa(\u5806\u6808\u6ea2\u51fa)\u95ee\u9898\u7684\u89e3\u51b3\u65b9\u6cd5"},"content":{"rendered":"<p>\n\t\u7a0b\u5e8f\u7684\u8fd0\u884c\u9700\u8981\u5185\u5b58\u3002\u53ea\u8981\u7a0b\u5e8f\u63d0\u51fa\u8981\u6c42\uff0c\u64cd\u4f5c\u7cfb\u7edf\u6216\u8005\u8fd0\u884c\u65f6\uff08runtime\uff09\u5c31\u5fc5\u987b\u4f9b\u7ed9\u5185\u5b58\u3002\n<\/p>\n<p>\n\t\u5bf9\u4e8e\u6301\u7eed\u8fd0\u884c\u7684\u670d\u52a1\u8fdb\u7a0b\uff08daemon\uff09\uff0c\u5fc5\u987b\u53ca\u65f6\u91ca\u653e\u4e0d\u518d\u7528\u5230\u7684\u5185\u5b58\u3002\u5426\u5219\uff0c\u5185\u5b58\u5360\u7528\u8d8a\u6765\u8d8a\u9ad8\uff0c\u8f7b\u5219\u5f71\u54cd\u7cfb\u7edf\u6027\u80fd\uff0c\u91cd\u5219\u5bfc\u81f4\u8fdb\u7a0b\u5d29\u6e83\u3002\n<\/p>\n<p>\n\t\u4e0d\u518d\u7528\u5230\u7684\u5185\u5b58\uff0c\u6ca1\u6709\u53ca\u65f6\u91ca\u653e\uff0c\u5c31\u53eb\u505a<a href=\"https:\/\/www.xttblog.com\/?tag=\u5185\u5b58\u6cc4\u6f0f\" name=\"\u5185\u5b58\u6cc4\u6f0f\" target=\"_blank\" title=\"\u5185\u5b58\u6cc4\u6f0f\">\u5185\u5b58\u6cc4\u6f0f<\/a>\uff08memory leak\uff09\u3002\n<\/p>\n<p>\n\t<img decoding=\"async\" alt=\"js\u5185\u5b58\u6cc4\u6f0f\" src=\"http:\/\/www.ruanyifeng.com\/blogimg\/asset\/2017\/bg2017041706.png\" style=\"width: 599px;height: 307px\" \/>\n<\/p>\n<p>\n\t\u5173\u4e8e<a href=\"https:\/\/www.xttblog.com\/?tag=\u5806\u6808\u6ea2\u51fa\" name=\"\u5806\u6808\u6ea2\u51fa\" target=\"_blank\" title=\"\u5806\u6808\u6ea2\u51fa\">\u5806\u6808\u7684\u6ea2\u51fa<\/a>\u95ee\u9898\uff0c\u5728 <a href=\"https:\/\/www.xttblog.com\/?tag=javascript\" name=\"javascript\" target=\"_blank\" title=\"javascript\">Javascript<\/a> \u65e5\u5e38\u5f00\u53d1\u4e2d\u5f88\u5e38\u89c1\uff0c\u7f51\u4e0a\u5173\u4e8e\u8fd9\u7c7b\u95ee\u9898\u8fd8\u662f\u6bd4\u8f83\u591a\u7684\u3002\u672c\u6587\u65e8\u5728\u63cf\u8ff0\u5982\u4f55\u89e3\u51b3\u6b64\u7c7b\u95ee\u9898\u3002\n<\/p>\n<p>\n\t\u9996\u5148\u770b\u4e00\u4e2a\u5b9e\u4f8b(\u5f53\u7136\u4f60\u53ef\u4ee5\u4f7f\u7528\u66f4\u5bb9\u6613\u7684\u65b9\u5f0f\u5b9e\u73b0\uff0c\u8fd9\u91cc\u6211\u4eec\u4ec5\u63a2\u8ba8\u9012\u5f52)\uff1a\n<\/p>\n<pre class=\"brush:jscript;\">\r\nfunction isEven (num) {\r\n    if (num === 0) {\r\n        return true;\r\n    }\r\n\t\/\/\u4e1a\u4f59\u8349\uff1awww.xttblog.com\r\n    if (num === 1) {\r\n        return false;\r\n    }\r\n\t\/\/\u4e1a\u4f59\u8349\uff1awww.xttblog.com\r\n    return isEven(Math.abs(num) - 2);\r\n}\r\n\/\/Outputs: true\r\nconsole.log(isEven(10));\r\n\/\/Outputs: false\r\nconsole.log(isEven(9));<\/pre>\n<p>\n\t\u4e0a\u9762\u7684\u7a0b\u5e8f\uff0c\u770b\u8d77\u6765\u6ca1\u6709\u4efb\u4f55\u95ee\u9898\uff0c\u8fd0\u884c\u51e0\u6b21\u4e5f\u770b\u4e0d\u51fa\u6765\u6709\u4ec0\u4e48\u95ee\u9898\u3002\u7136\u800c\u5f53\u6211\u4eec\u8ba9\u4e0a\u9762\u7684\u53c2\u6570\u6539\u4e3a 10000 \u7684\u65f6\u5019\uff0c\u4f1a\u53d1\u73b0\u6709<a href=\"https:\/\/www.xttblog.com\/?tag=\u5185\u5b58\u6cc4\u6f0f\" name=\"\u5185\u5b58\u6cc4\u6f0f\" target=\"_blank\" title=\"\u5185\u5b58\u6cc4\u6f0f\">\u5185\u5b58\u6cc4\u6f0f<\/a>\uff08<a href=\"https:\/\/www.xttblog.com\/?tag=\u5806\u6808\u6ea2\u51fa\" name=\"\u5806\u6808\u6ea2\u51fa\" target=\"_blank\" title=\"\u5806\u6808\u6ea2\u51fa\">\u5806\u6808\u6ea2\u51fa<\/a>\uff09\u95ee\u9898\u3002\n<\/p>\n<pre class=\"brush:jscript;\">\r\n\/\/\u4e0d\u540c\u7684javascript\u5f15\u64ce\u62a5\u9519\u53ef\u80fd\u4e0d\u540c\r\n\/\/Outputs: Uncaught RangeError: Maximum call stack size exceeded \r\nconsole.log(isEven(10000));<\/pre>\n<p>\n\t\u539f\u56e0\u662f\u6bcf\u6b21\u6267\u884c\u4ee3\u7801\u65f6\uff0c\u90fd\u4f1a\u5206\u914d\u4e00\u5b9a\u5c3a\u5bf8\u7684\u6808\u7a7a\u95f4\uff08Windows\u7cfb\u7edf\u4e2d\u4e3a1M\uff09\uff0c\u6bcf\u6b21\u65b9\u6cd5\u8c03\u7528\u65f6\u90fd\u4f1a\u5728\u6808\u91cc\u50a8\u5b58\u4e00\u5b9a\u4fe1\u606f\uff08\u5982\u53c2\u6570\u3001\u5c40\u90e8\u53d8\u91cf\u3001\u8fd4\u56de\u503c\u7b49\u7b49\uff09\uff0c\u8fd9\u4e9b\u4fe1\u606f\u518d\u5c11\u4e5f\u4f1a\u5360\u7528\u4e00\u5b9a\u7a7a\u95f4\uff0c\u6210\u5343\u4e0a\u4e07\u4e2a\u6b64\u7c7b\u7a7a\u95f4\u7d2f\u79ef\u8d77\u6765\uff0c\u81ea\u7136\u5c31\u8d85\u8fc7\u7ebf\u7a0b\u7684\u6808\u7a7a\u95f4\u4e86\u3002\u90a3\u4e48\u5982\u4f55\u89e3\u51b3\u6b64\u7c7b\u95ee\u9898\uff1f\n<\/p>\n<h2>\n\t\u4f7f\u7528\u95ed\u5305<br \/>\n<\/h2>\n<pre class=\"brush:jscript;\">\r\nfunction isEven (num) {\r\n    if (num === 0) {\r\n        return true;\r\n    }\r\n    if (num === 1) {\r\n        return false;\r\n    }\r\n\t\/\/\u4e1a\u4f59\u8349\uff1awww.xttblog.com\r\n    return function() {\r\n        return isEven(Math.abs(num) - 2);\r\n    }\r\n}\r\n\/\/Outputs: true\r\nconsole.log(isEven(4)()());<\/pre>\n<p>\n\t\u6b64\u65f6\u6bcf\u6b21\u8c03\u7528\u65f6\uff0c\u8fd4\u56de\u4e00\u4e2a\u533f\u540d\u51fd\u6570\uff0c\u533f\u540d\u51fd\u6570\u6267\u884c\u76f8\u5173\u7684\u53c2\u6570\u548c\u5c40\u90e8\u53d8\u91cf\u5c06\u4f1a\u91ca\u653e\uff0c\u4e0d\u4f1a\u989d\u5916\u589e\u52a0\u5806\u6808\u5927\u5c0f\u3002\n<\/p>\n<h2>\n\t\u4f18\u5316\u8c03\u7528<br \/>\n<\/h2>\n<p>\n\t\u4e0a\u4f8b\u8c03\u7528\u6bd4\u8f83\u9ebb\u70e6\uff0c\u4f18\u5316\u5982\u4e0b\uff1a\n<\/p>\n<pre class=\"brush:jscript;\">\r\nfunction isEven (num) {\r\n    if (num === 0) {\r\n        return true;\r\n    }\r\n    if (num === 1) {\r\n        return false;\r\n    }\r\n    return function() {\r\n        return isEven(Math.abs(num) - 2);\r\n    }\r\n}\r\nfunction trampoline (func, arg) {\r\n    var value = func(arg);\r\n    while(typeof value === &quot;function&quot;) {\r\n        value = value();\r\n    }\r\n    return value;\r\n}\r\n\/\/Outputs: true\r\nconsole.log(trampoline(isEven, 10000));\r\n\/\/\u4e1a\u4f59\u8349\uff1awww.xttblog.com\r\n\/\/Outputs: false\r\nconsole.log(trampoline(isEven, 10001));<\/pre>\n<p>\n\t\u901a\u8fc7\u4e0a\u9762\u7684\u4e24\u79cd\u529e\u6cd5\uff0c\u90fd\u53ef\u4ee5\u89e3\u51b3\u8fd9\u7c7bjs\u9012\u5f52\u51fd\u6570\u9020\u6210\u7684<a href=\"https:\/\/www.xttblog.com\/?tag=\u5185\u5b58\u6cc4\u6f0f\" name=\"\u5185\u5b58\u6cc4\u6f0f\" target=\"_blank\" title=\"\u5185\u5b58\u6cc4\u6f0f\">\u5185\u5b58\u6cc4\u6f0f<\/a>\uff08<a href=\"https:\/\/www.xttblog.com\/?tag=\u5806\u6808\u6ea2\u51fa\" name=\"\u5806\u6808\u6ea2\u51fa\" target=\"_blank\" title=\"\u5806\u6808\u6ea2\u51fa\">\u5806\u6808\u6ea2\u51fa<\/a>\uff09\u95ee\u9898\u3002<\/p>\n<!-- <audio autoplay=\"autoplay\" height=\"100\" width=\"100\" hidden=\"true\"><source src=\"https:\/\/c.coka.la\/orAvPR.mp3\" type=\"audio\/mp3\"\/><\/audio><img decoding=\"async\" src=\"https:\/\/images.weserv.nl\/?url=https:\/\/ws3.sinaimg.cn\/large\/8fa5dcfcgy1g4em3rzar5j20ib089ae0.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/> --><p><!-- <img decoding=\"async\" src=\"https:\/\/raw.githubusercontent.com\/xmt1139057136\/xttblog\/master\/8fa5dcfcgy1g4em3rzar5j20ib089ae0.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/>--><img decoding=\"async\" src=\"https:\/\/saas.51autoparts.cn\/api\/admin\/sys-file\/oss\/file?fileName=84db51f8561e448f9a4fda2eff0811f9.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/><\/p><p>\u6700\u540e\uff0c\u6b22\u8fce\u5173\u6ce8\u6211\u7684\u4e2a\u4eba\u5fae\u4fe1\u516c\u4f17\u53f7\uff1a\u4e1a\u4f59\u8349\uff08yyucao\uff09\uff01<b>\u53ef\u52a0\u4f5c\u8005\u5fae\u4fe1\u53f7\uff1a<font color=\"red\">xttblog2<\/font>\u3002\u5907\u6ce8\uff1a\u201c1\u201d\uff0c\u6dfb\u52a0\u535a\u4e3b\u5fae\u4fe1\u62c9\u4f60\u8fdb\u5fae\u4fe1\u7fa4\u3002\u5907\u6ce8\u9519\u8bef\u4e0d\u4f1a\u540c\u610f\u597d\u53cb\u7533\u8bf7\u3002<\/b>\u518d\u6b21\u611f\u8c22\u60a8\u7684\u5173\u6ce8\uff01\u540e\u7eed\u6709\u7cbe\u5f69\u5185\u5bb9\u4f1a\u7b2c\u4e00\u65f6\u95f4\u53d1\u7ed9\u60a8\uff01\u539f\u521b\u6587\u7ae0\u6295\u7a3f\u8bf7\u53d1\u9001\u81f3532009913@qq.com\u90ae\u7bb1\u3002\u5546\u52a1\u5408\u4f5c\u4e5f\u53ef\u6dfb\u52a0\u4f5c\u8005\u5fae\u4fe1\u8fdb\u884c\u8054\u7cfb\uff01<\/p><p>\u672c\u6587\u539f\u6587\u51fa\u5904\uff1a<a href=\"https:\/\/www.xttblog.com\">\u4e1a\u4f59\u8349<\/a>\uff1a &raquo; <a href=\"https:\/\/www.xttblog.com\/?p=2045\">js\u9012\u5f52\u51fd\u6570\u9020\u6210\u7684\u5185\u5b58\u6ea2\u51fa(\u5806\u6808\u6ea2\u51fa)\u95ee\u9898\u7684\u89e3\u51b3\u65b9\u6cd5<\/a><\/p><!-- <div class=\"bd-reward-stl\"><button id=\"bdRewardBtn\"><span><\/span><\/button><\/div> -->","protected":false},"excerpt":{"rendered":"<p>\u7a0b\u5e8f\u7684\u8fd0\u884c\u9700\u8981\u5185\u5b58\u3002\u53ea\u8981\u7a0b\u5e8f\u63d0\u51fa\u8981\u6c42\uff0c\u64cd\u4f5c\u7cfb\u7edf\u6216\u8005\u8fd0\u884c\u65f6\uff08runtime\uff09\u5c31\u5fc5\u987b\u4f9b\u7ed9\u5185\u5b58\u3002 \u5bf9\u4e8e\u6301\u7eed\u8fd0\u884c\u7684\u670d\u52a1 [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[1106,1532,1531],"class_list":["post-2045","post","type-post","status-publish","format-standard","hentry","category-java","tag-javascript","tag-1532","tag-1531"],"_links":{"self":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/2045","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2045"}],"version-history":[{"count":1,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/2045\/revisions"}],"predecessor-version":[{"id":2155,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/2045\/revisions\/2155"}],"wp:attachment":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2045"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2045"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2045"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}