{"id":3725,"date":"2026-01-14T17:35:15","date_gmt":"2026-01-14T09:35:15","guid":{"rendered":"https:\/\/ufqi.com\/blog\/?p=3725"},"modified":"2026-01-15T08:07:01","modified_gmt":"2026-01-15T00:07:01","slug":"javascript-gc-var-let-const","status":"publish","type":"post","link":"https:\/\/ufqi.com\/blog\/javascript-gc-var-let-const\/","title":{"rendered":"JavaScript \u5185\u5b58\u4f18\u5316 GC \u56de\u6536 \u4e0e var let \u548c const 1\/2"},"content":{"rendered":"\n<p>2026\u5e74\u5143\u65e6\u4ee5\u6765\uff0c\u4e2d\u80a1\u5e02\u573a\u7ee7\u7eed\u725b\u5e02\u4e0a\u6da8\u3002\u6211\u4eec\u89c2\u6d4b\u7684 <a href=\"https:\/\/ufqi.com\/?mod=finance\" target=\"_blank\" rel=\"noreferrer noopener\">UfqiFina \u6709\u798f\u91d1\u878d<\/a> \u91cc\u7684\u57fa\u91d1\u4e5f\u51fa\u73b0\u4e86\u5927\u91cf\u5448\u73b0\u4e70\u5165\u4fe1\u53f7\u53ef\u9009\u6807\u7684\uff0c\u968f\u4e4b\u800c\u6765\u7684\u662f\u8ba1\u7b97\u91cf\u4e5f\u9010\u6e10\u589e\u5927\u3002<\/p>\n\n\n\n<p>\u7531\u4e8e\u5728\u8bbe\u8ba1\u4e0a\uff0cUfqiFina \u7684\u4e00\u4e9b\u8ba1\u7b97\u9700\u8981\u653e\u5230\u524d\u7aef\u6d4f\u89c8\u5668\u5185\u8fdb\u884c\uff0c\u968f\u7740\u8ba1\u7b97\u91cf\u7684\u589e\u5927\uff0c\u4f9d\u8d56 JavaScript \u7684\u7a0b\u5e8f\u5728\u8fd0\u884c\u4e2d\u9010\u6e10\u66dd\u9732\u51fa\u8fd0\u884c\u65f6\u95f4\u957f\uff0c\u5185\u5b58\u6d88\u8017\u9010\u6b65\u589e\u52a0\u7684\u95ee\u9898\u3002\u8fd9\u5bf9\u8ba1\u7b97\u8d44\u6e90\u7684\u9700\u6c42\u5b9b\u5982\u8fc7\u53bb\u4e00\u5e74\u4e2d\u56fd\u8bc1\u5238\u5e02\u573a\u4e00\u6837\uff0c\u9010\u6b65\u4e0a\u626c\u3002\u8feb\u4e0d\u5f97\u5df2\uff0c\u6211\u4eec\u501f\u52a9 AI\u52a9\u624b \u6296\u97f3\u8c46\u5305 \uff0c\u6709\u9488\u5bf9\u6027\u5730\u5bf9 UfqiFina \u91d1\u878d\u6d89\u53ca\u5230\u7684 Hanjst \u8fdb\u884c\u6027\u80fd\u53ca\u5185\u5b58\u65b9\u9762\u7684\u4f18\u5316\u548c\u63d0\u5347\u3002<\/p>\n\n\n\n<p>\u5927\u81f4\u7ed3\u8bba\uff0c\u901a\u8fc7\u4e0b\u9762\u8fd9\u4e9b\u6539\u8fdb\uff0cUfqiFina \u5b9e\u73b0\u5bf9\u4e00\u7ec4\u5019\u9009\u57fa\u91d1\u8fdb\u884c\u626b\u63cf\u65f6\uff0c\u6240\u5360\u7528\u7684\u5185\u5b58\u7531\u4fee\u6539\u524d\u7684:<br \/>\u5cf0\u503c 3000MB~5000MB \uff0c\u964d\u4f4e\u4e3a 500MB~1500MB \u3002\u5927\u7ea6 2 \u500d \u81f3 10 \u500d \u7684\u4f18\u5316\u63d0\u5347\u3002<br \/>Hanjst \u6b64\u756a\u7684\u4fee\u6539\u4e3b\u8981\u662f\u5728\u53d8\u91cf\u58f0\u660e\u548c\u4f7f\u7528\u4e2d \u4f7f\u7528 let \/ const \u66ff\u6362 var \uff0c\u5e76\u589e\u52a0\u76f8\u5e94\u5927\u6570\u636e\u5bf9\u8c61\u7684\u663e\u5f0f\u9500\u6bc1\u58f0\u660e\u3002<\/p>\n\n\n\n<p><strong>A1. JavaScript \u7684\u5185\u5b58\u7ba1\u7406 GC \u56de\u6536<\/strong><\/p>\n\n\n\n<p>JavaScript \u662f<strong>\u81ea\u52a8\u5783\u573e\u56de\u6536<\/strong>\u7684\u8bed\u8a00\uff0c\u4e0d\u9700\u8981\u624b\u52a8\u5206\u914d \/ \u91ca\u653e\u5185\u5b58\uff0cGC \u7684\u6838\u5fc3\u76ee\u6807\u662f\u56de\u6536 \u201c\u4e0d\u518d\u88ab\u4f7f\u7528\u201d \u7684\u5185\u5b58\u7a7a\u95f4\u3002GC \u81ea\u52a8\u6267\u884c\uff0c\u4f46\u5982\u679c\u4ee3\u7801\u5199\u5f97\u4e0d\u5f53\uff0c\u4f1a\u5bfc\u81f4 \u201c\u5185\u5b58\u6cc4\u6f0f\u201d\uff08\u672c\u8be5\u56de\u6536\u7684\u5185\u5b58\u65e0\u6cd5\u88ab\u56de\u6536\uff09\uff0c\u6700\u7ec8\u5f15\u53d1\u9875\u9762\u5361\u987f\u3001\u5d29\u6e83\u3002\u4ee5\u4e0b\u662f\u6700\u5b9e\u7528\u7684\u4f18\u5316\u624b\u6bb5\uff1a<\/p>\n\n\n\n<h4>1. \u907f\u514d\u610f\u5916\u7684\u5168\u5c40\u53d8\u91cf<\/h4>\n\n\n\n<p>\u5168\u5c40\u53d8\u91cf\u6302\u8f7d\u5728 <code>window<\/code> \u4e0a\uff0c\u751f\u547d\u5468\u671f\u548c\u9875\u9762\u4e00\u81f4\uff0c\u4e0d\u4f1a\u88ab GC \u56de\u6536\uff0c\u662f\u6700\u5e38\u89c1\u7684\u5185\u5b58\u6cc4\u6f0f\u539f\u56e0\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ \u9519\u8bef\u793a\u4f8b\uff1a\u672a\u58f0\u660e\u7684\u53d8\u91cf\u9690\u5f0f\u6210\u4e3a\u5168\u5c40\u53d8\u91cf\nfunction test() {\n  \/\/ \u672a\u7528 let\/const\/var \u58f0\u660e\uff0c\u4f1a\u6302\u5230 window \u4e0a\n  unGlobalVar = { data: new Array(1000000) }; \n}\ntest(); \/\/ \u8fd9\u4e2a\u5927\u6570\u7ec4\u6c38\u8fdc\u4e0d\u4f1a\u88ab\u56de\u6536\n\n\/\/ \u6b63\u786e\u505a\u6cd5\uff1a\n\/\/ 1. \u4f7f\u7528 let\/const \u9650\u5236\u4f5c\u7528\u57df\nfunction test() {\n  const localVar = { data: new Array(1000000) };\n  \/\/ \u7528\u5b8c\u540e\u624b\u52a8\u7f6e null\uff08\u975e\u5fc5\u9700\uff0c\u4f46\u5927\u5bf9\u8c61\u5efa\u8bae\uff09\n  localVar = null; \n}<\/code><\/pre>\n\n\n\n<h4>2. \u53ca\u65f6\u6e05\u7406 DOM \u5f15\u7528<\/h4>\n\n\n\n<p>\u5982\u679c\u53d8\u91cf\u4fdd\u5b58\u4e86 DOM \u5143\u7d20\u7684\u5f15\u7528\uff0c\u5373\u4f7f DOM \u88ab\u4ece\u9875\u9762\u79fb\u9664\uff0c\u8be5\u5f15\u7528\u4ecd\u4f1a\u963b\u6b62 GC \u56de\u6536\uff0c\u5bfc\u81f4\u5185\u5b58\u6cc4\u6f0f\u3002javascript\u8fd0\u884c<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ \u9519\u8bef\u793a\u4f8b\uff1aDOM \u88ab\u5220\u9664\u4f46\u5f15\u7528\u8fd8\u5728\nconst btn = document.getElementById('btn');\ndocument.body.removeChild(btn); \n\/\/ btn \u53d8\u91cf\u4ecd\u5f15\u7528\u8be5 DOM\uff0c\u5185\u5b58\u65e0\u6cd5\u56de\u6536\n\n\/\/ \u6b63\u786e\u505a\u6cd5\uff1a\u5220\u9664 DOM \u540e\u6e05\u7a7a\u5f15\u7528\nconst btn = document.getElementById('btn');\ndocument.body.removeChild(btn);\nbtn = null; \/\/ \u89e3\u9664\u5f15\u7528\uff0cGC \u53ef\u56de\u6536\n<\/code><\/pre>\n\n\n\n<h4>3. \u6e05\u9664\u5b9a\u65f6\u5668 \/ \u4e8b\u4ef6\u76d1\u542c\u5668<\/h4>\n\n\n\n<p>\u5b9a\u65f6\u5668\uff08<code>setInterval\/setTimeout<\/code>\uff09\u548c\u4e8b\u4ef6\u76d1\u542c\u5668\u5982\u679c\u4e0d\u624b\u52a8\u6e05\u9664\uff0c\u5176\u56de\u8c03\u51fd\u6570\u53ca\u5185\u90e8\u5f15\u7528\u7684\u5bf9\u8c61\u6c38\u8fdc\u4e0d\u4f1a\u88ab\u56de\u6536\u3002javascript\u8fd0\u884c<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ \u9519\u8bef\u793a\u4f8b\uff1a\u5b9a\u65f6\u5668\u672a\u6e05\u9664\nsetInterval(() =&gt; {\n  console.log('\u672a\u6e05\u9664\u7684\u5b9a\u65f6\u5668');\n  \/\/ \u5185\u90e8\u5f15\u7528\u7684\u5927\u5bf9\u8c61\u6c38\u8fdc\u65e0\u6cd5\u56de\u6536\n}, 1000); \n\n\/\/ \u6b63\u786e\u505a\u6cd5\uff1a\u4fdd\u5b58\u5b9a\u65f6\u5668ID\uff0c\u4e0d\u7528\u65f6\u6e05\u9664\nconst timerId = setInterval(() =&gt; {\n  console.log('\u53ef\u56de\u6536\u7684\u5b9a\u65f6\u5668');\n}, 1000);\n\/\/ \u9875\u9762\u5378\u8f7d\/\u4e0d\u9700\u8981\u65f6\u6e05\u9664\nclearInterval(timerId);<\/code><\/pre>\n\n\n\n<h4>4. \u4f7f\u7528\u5f31\u5f15\u7528\uff08WeakMap\/WeakSet\uff09<\/h4>\n\n\n\n<p><code>Map\/Set<\/code> \u7684\u952e\u662f<strong>\u5f3a\u5f15\u7528<\/strong>\uff0c\u4f1a\u963b\u6b62 GC \u56de\u6536\uff1b\u800c <code>WeakMap\/WeakSet<\/code> \u7684\u952e\u662f<strong>\u5f31\u5f15\u7528<\/strong>\uff0c\u4e0d\u4f1a\u963b\u6b62 GC \u56de\u6536\uff0c\u9002\u5408\u5b58\u50a8\u4e34\u65f6\u5173\u8054\u6570\u636e\u3002javascript\u8fd0\u884c<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ Map\uff08\u5f3a\u5f15\u7528\uff09\uff1a\u5373\u4f7f dom \u88ab\u5220\u9664\uff0cMap \u4ecd\u5f15\u7528\u5b83\uff0c\u65e0\u6cd5\u56de\u6536\nconst map = new Map();\nconst dom = document.getElementById('dom');\nmap.set(dom, '\u9644\u52a0\u6570\u636e');\ndocument.body.removeChild(dom); \n\/\/ dom \u4ecd\u88ab Map \u5f15\u7528\uff0c\u5185\u5b58\u6cc4\u6f0f\n\n\/\/ WeakMap\uff08\u5f31\u5f15\u7528\uff09\uff1adom \u88ab\u5220\u9664\u540e\uff0cGC \u81ea\u52a8\u56de\u6536\u5bf9\u5e94\u7684\u952e\u503c\u5bf9\nconst weakMap = new WeakMap();\nconst dom = document.getElementById('dom');\nweakMap.set(dom, '\u9644\u52a0\u6570\u636e');\ndocument.body.removeChild(dom); \n\/\/ dom \u65e0\u5176\u4ed6\u5f15\u7528\uff0cGC \u4f1a\u56de\u6536 dom \u53ca weakMap \u4e2d\u7684\u5bf9\u5e94\u6761\u76ee<\/code><\/pre>\n\n\n\n<p>\u4ee5\u4e0a4\u4e2a\u65b9\u9762\u5bf9\u6539\u5584 JavaScript \u8fd0\u884c\u65f6\u7684\u5185\u5b58\u5360\u7528\u5206\u6790\uff0c\u90fd\u6709\u4e00\u5b9a\u7684\u9053\u7406\u3002\u5728 UfqiFina \u7684\u5b9e\u8df5\u4e2d\uff0c\u4e3b\u8981\u8fdb\u884c\u4e86\u524d 3 \u9879\u7684\u6539\u8fdb\uff0c\u66ff\u6362\u53d8\u91cf\u7684\u58f0\u660e\u5173\u952e\u8bcd\uff0c\u53ca\u65f6\u6e05\u7406 DOM \u5f15\u7528 \u548c \u6e05\u9664 \u5b9a\u65f6\u5668\uff0c<\/p>\n\n\n\n<p>\u5148\u8bf4\u6e05\u9664 JavaScript \u4e2d\u7684\u5b9a\u65f6\u5668\uff0cJavaScript \u672c\u8eab<strong>\u6ca1\u6709\u5185\u7f6e API \u76f4\u63a5\u83b7\u53d6\u6240\u6709\u5df2\u521b\u5efa\u7684\u5b9a\u65f6\u5668 ID<\/strong>\uff0c\u56e0\u4e3a\u6d4f\u89c8\u5668\u4f1a\u81ea\u52a8\u5206\u914d\u9012\u589e\u7684\u6570\u5b57 ID\uff0c\u4f46\u4e0d\u4f1a\u66b4\u9732\u6240\u6709 ID \u7684\u5217\u8868\u3002\u56e0\u6b64\u6838\u5fc3\u601d\u8def\u662f\uff1a<\/p>\n\n\n\n<ol><li><strong>\u62e6\u622a\u5e76\u91cd\u5199\u539f\u751f\u5b9a\u65f6\u5668\u65b9\u6cd5<\/strong>\uff0c\u8bb0\u5f55\u6240\u6709\u521b\u5efa\u7684\u5b9a\u65f6\u5668 ID\uff1b<\/li><li><strong>\u904d\u5386\u8bb0\u5f55\u7684 ID \u7edf\u4e00\u6e05\u9664<\/strong>\uff0c\u540c\u65f6\u5229\u7528\u6d4f\u89c8\u5668\u7684\u517c\u5bb9\u7279\u6027\uff08<code>clearTimeout<\/code> \u548c <code>clearInterval<\/code> \u53ef\u4ee5\u4e92\u76f8\u66ff\u4ee3\uff0c\u6e05\u9664\u5bf9\u65b9\u7c7b\u578b\u7684\u5b9a\u65f6\u5668\uff09\u7b80\u5316\u903b\u8f91\u3002<\/li><\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ 1. \u4fdd\u5b58\u539f\u751f\u7684 setTimeout\/setInterval \u65b9\u6cd5\uff0c\u907f\u514d\u8986\u76d6\u540e\u65e0\u6cd5\u6062\u590d\nconst nativeSetTimeout = window.setTimeout;\nconst nativeSetInterval = window.setInterval;\n\n\/\/ 2. \u5b9a\u4e49\u6570\u7ec4\u5b58\u50a8\u6240\u6709\u5b9a\u65f6\u5668 ID\nconst timerIds = new Set(); \/\/ \u4f7f\u7528 Set \u907f\u514d\u91cd\u590d ID\uff08\u867d\u7136\u7406\u8bba\u4e0a ID \u4e0d\u4f1a\u91cd\u590d\uff0c\u4f46\u589e\u52a0\u5bb9\u9519\uff09\n\n\/\/ 3. \u91cd\u5199 setTimeout\uff0c\u8bb0\u5f55\u6240\u6709\u521b\u5efa\u7684 timer ID\nwindow.setTimeout = function (callback, delay, ...args) {\n  const timerId = nativeSetTimeout(callback, delay, ...args);\n  timerIds.add(timerId); \/\/ \u8bb0\u5f55 ID\n  return timerId; \/\/ \u4fdd\u7559\u539f\u751f\u8fd4\u56de\u503c\uff0c\u4e0d\u5f71\u54cd\u539f\u6709\u903b\u8f91\n};\n\n\/\/ 4. \u91cd\u5199 setInterval\uff0c\u8bb0\u5f55\u6240\u6709\u521b\u5efa\u7684 interval ID\nwindow.setInterval = function (callback, delay, ...args) {\n  const intervalId = nativeSetInterval(callback, delay, ...args);\n  timerIds.add(intervalId); \/\/ \u8bb0\u5f55 ID\n  return intervalId; \/\/ \u4fdd\u7559\u539f\u751f\u8fd4\u56de\u503c\n};\n\n\/\/ 5. \u6838\u5fc3\u51fd\u6570\uff1a\u4e00\u6b21\u6027\u6e05\u9664\u6240\u6709\u5b9a\u65f6\u5668\uff08timer + interval\uff09\nfunction clearAllTimers() {\n  \/\/ \u904d\u5386\u6240\u6709\u8bb0\u5f55\u7684 ID \u5e76\u6e05\u9664\n  for (const id of timerIds) {\n    try {\n      \/\/ \u517c\u5bb9\u5904\u7406\uff1aclearTimeout \u53ef\u6e05\u9664 interval\uff0cclearInterval \u4e5f\u53ef\u6e05\u9664 timeout\n      clearTimeout(id); \n      clearInterval(id);\n    } catch (e) {\n      \/\/ \u5ffd\u7565\u65e0\u6548 ID \u7684\u62a5\u9519\uff08\u6bd4\u5982\u5df2\u88ab\u624b\u52a8\u6e05\u9664\u7684 ID\uff09\n      console.debug('\u6e05\u9664\u5b9a\u65f6\u5668 ID \u5931\u8d25:', id, e);\n    }\n  }\n  \/\/ \u6e05\u7a7a\u8bb0\u5f55\u7684 ID \u5217\u8868\uff0c\u907f\u514d\u91cd\u590d\u6e05\u9664\n  timerIds.clear();\n}\n\n\/\/ 6. \u53ef\u9009\uff1a\u6062\u590d\u539f\u751f\u5b9a\u65f6\u5668\u65b9\u6cd5\uff08\u5982\u679c\u9700\u8981\uff09\nfunction restoreNativeTimers() {\n  window.setTimeout = nativeSetTimeout;\n  window.setInterval = nativeSetInterval;\n  timerIds.clear();\n}<\/code><\/pre>\n\n\n\n<p>\u4e0b\u4e00\u7bc7\u7ee7\u7eed\u5199 \u4f7f\u7528 let \/ const \u66ff\u6362 var .<\/p>\n","protected":false},"excerpt":{"rendered":"<p>2026\u5e74\u5143\u65e6\u4ee5\u6765\uff0c\u4e2d\u80a1\u5e02\u573a\u7ee7\u7eed\u725b\u5e02\u4e0a\u6da8\u3002\u6211\u4eec\u89c2\u6d4b\u7684 UfqiFina \u6709\u798f\u91d1\u878d &hellip; <a href=\"https:\/\/ufqi.com\/blog\/javascript-gc-var-let-const\/\">\u7ee7\u7eed\u9605\u8bfb <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[96,301,2],"tags":[836,316,837,838],"_links":{"self":[{"href":"https:\/\/ufqi.com\/blog\/wp-json\/wp\/v2\/posts\/3725"}],"collection":[{"href":"https:\/\/ufqi.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ufqi.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ufqi.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/ufqi.com\/blog\/wp-json\/wp\/v2\/comments?post=3725"}],"version-history":[{"count":4,"href":"https:\/\/ufqi.com\/blog\/wp-json\/wp\/v2\/posts\/3725\/revisions"}],"predecessor-version":[{"id":3730,"href":"https:\/\/ufqi.com\/blog\/wp-json\/wp\/v2\/posts\/3725\/revisions\/3730"}],"wp:attachment":[{"href":"https:\/\/ufqi.com\/blog\/wp-json\/wp\/v2\/media?parent=3725"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ufqi.com\/blog\/wp-json\/wp\/v2\/categories?post=3725"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ufqi.com\/blog\/wp-json\/wp\/v2\/tags?post=3725"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}