{"id":235,"date":"2026-03-09T22:25:18","date_gmt":"2026-03-09T14:25:18","guid":{"rendered":"https:\/\/www.resilence.cn\/?p=235"},"modified":"2026-03-09T22:25:18","modified_gmt":"2026-03-09T14:25:18","slug":"day-8-%e4%bd%9c%e7%94%a8%e5%9f%9f%e4%b8%8e%e9%97%ad%e5%8c%85","status":"publish","type":"post","link":"https:\/\/www.resilence.cn\/?p=235","title":{"rendered":"Day-8-\u4f5c\u7528\u57df\u4e0e\u95ed\u5305"},"content":{"rendered":"<h1>Day 8: \u4f5c\u7528\u57df\u4e0e\u95ed\u5305<\/h1>\n<h2>\ud83c\udfaf \u5b66\u4e60\u76ee\u6807<\/h2>\n<ul>\n<li>\u7406\u89e3 \u4f5c\u7528\u57df\u4e0e\u95ed\u5305 \u7684\u6838\u5fc3\u6982\u5ff5\u548c\u5de5\u4f5c\u539f\u7406<\/li>\n<li>\u638c\u63e1\u76f8\u5173\u8bed\u6cd5\u548c\u6700\u4f73\u5b9e\u8df5<\/li>\n<li>\u5b66\u4f1a\u5728\u5b9e\u9645\u9879\u76ee\u4e2d\u5e94\u7528 \u4f5c\u7528\u57df\u4e0e\u95ed\u5305<\/li>\n<\/ul>\n<h2>\ud83d\udca1 \u6838\u5fc3\u6982\u5ff5<\/h2>\n<p>\u4f5c\u7528\u57df\uff08Scope\uff09\u51b3\u5b9a\u4e86\u53d8\u91cf\u7684\u53ef\u8bbf\u95ee\u8303\u56f4\uff0c\u800c\u95ed\u5305\uff08Closure\uff09\u662fJavaScript\u4e2d\u6700\u5f3a\u5927\u7684\u7279\u6027\u4e4b\u4e00\u3002\u7406\u89e3\u8fd9\u4e24\u4e2a\u6982\u5ff5\u5bf9\u4e8e\u7f16\u5199\u9ad8\u8d28\u91cf\u7684JavaScript\u4ee3\u7801\u81f3\u5173\u91cd\u8981\u3002<\/p>\n<h3>\u5173\u952e\u6982\u5ff5<\/h3>\n<h4>1. \u4f5c\u7528\u57df\uff08Scope\uff09<\/h4>\n<p>\u4f5c\u7528\u57df\u662f\u53d8\u91cf\u548c\u51fd\u6570\u7684\u53ef\u8bbf\u95ee\u8303\u56f4\u3002JavaScript\u6709\u4e09\u79cd\u4f5c\u7528\u57df\uff1a<\/p>\n<ol>\n<li><strong>\u5168\u5c40\u4f5c\u7528\u57df<\/strong>\uff1a\u5728\u4efb\u4f55\u51fd\u6570\u5916\u58f0\u660e\u7684\u53d8\u91cf<\/li>\n<\/ol>\n<pre><code class=\"language-javascript\">let globalVar = &quot;\u5168\u5c40\u53d8\u91cf&quot;;\nfunction test() {\n    console.log(globalVar);  \/\/ \u53ef\u4ee5\u8bbf\u95ee\n}\n<\/code><\/pre>\n<ol start=\"2\">\n<li><strong>\u51fd\u6570\u4f5c\u7528\u57df<\/strong>\uff1a\u5728\u51fd\u6570\u5185\u58f0\u660e\u7684\u53d8\u91cf\uff0c\u53ea\u5728\u51fd\u6570\u5185\u53ef\u8bbf\u95ee<\/li>\n<\/ol>\n<pre><code class=\"language-javascript\">function test() {\n    let localVar = &quot;\u5c40\u90e8\u53d8\u91cf&quot;;\n    console.log(localVar);  \/\/ \u53ef\u4ee5\u8bbf\u95ee\n}\nconsole.log(localVar);  \/\/ \u9519\u8bef\uff1a\u65e0\u6cd5\u8bbf\u95ee\n<\/code><\/pre>\n<ol start=\"3\">\n<li><strong>\u5757\u7ea7\u4f5c\u7528\u57df<\/strong>\uff1alet\/const\u5728{}\u5757\u5185\u58f0\u660e\u7684\u53d8\u91cf<\/li>\n<\/ol>\n<pre><code class=\"language-javascript\">if (true) {\n    let blockVar = &quot;\u5757\u7ea7\u53d8\u91cf&quot;;\n    console.log(blockVar);  \/\/ \u53ef\u4ee5\u8bbf\u95ee\n}\nconsole.log(blockVar);  \/\/ \u9519\u8bef\uff1a\u65e0\u6cd5\u8bbf\u95ee\n<\/code><\/pre>\n<h4>2. \u53d8\u91cf\u63d0\u5347\uff08Hoisting\uff09<\/h4>\n<p>var\u58f0\u660e\u7684\u53d8\u91cf\u4f1a\u88ab\u63d0\u5347\u5230\u51fd\u6570\u9876\u90e8\uff0c\u4f46\u8d4b\u503c\u4e0d\u4f1a\u63d0\u5347\uff1a<\/p>\n<pre><code class=\"language-javascript\">console.log(myVar);  \/\/ undefined\uff08\u4e0d\u662f\u9519\u8bef\uff09\nvar myVar = 10;\n\n\/\/ \u5b9e\u9645\u6267\u884c\u987a\u5e8f\nvar myVar;           \/\/ \u58f0\u660e\u63d0\u5347\nconsole.log(myVar);  \/\/ undefined\nmyVar = 10;          \/\/ \u8d4b\u503c\u7559\u5728\u539f\u5730\n<\/code><\/pre>\n<p>let\/const\u4e0d\u4f1a\u88ab\u63d0\u5347\uff1a<\/p>\n<pre><code class=\"language-javascript\">console.log(myLet);  \/\/ ReferenceError\nlet myLet = 10;\n<\/code><\/pre>\n<p><strong>\u6700\u4f73\u5b9e\u8df5<\/strong>\uff1a\u59cb\u7ec8\u5728\u4f5c\u7528\u57df\u9876\u90e8\u58f0\u660e\u53d8\u91cf\uff0c\u907f\u514d\u6df7\u6dc6\u3002<\/p>\n<h4>3. \u95ed\u5305\uff08Closure\uff09<\/h4>\n<p>\u95ed\u5305\u662f\u51fd\u6570\u548c\u5176\u8bcd\u6cd5\u73af\u5883\u7684\u7ec4\u5408\u3002\u7b80\u5355\u6765\u8bf4\uff0c\u5185\u90e8\u51fd\u6570\u53ef\u4ee5\u8bbf\u95ee\u5916\u90e8\u51fd\u6570\u7684\u53d8\u91cf\uff0c\u5373\u4f7f\u5916\u90e8\u51fd\u6570\u5df2\u7ecf\u8fd4\u56de\u3002<\/p>\n<p><strong>\u95ed\u5305\u793a\u4f8b<\/strong>\uff1a<\/p>\n<pre><code class=\"language-javascript\">function createCounter() {\n    let count = 0;  \/\/ \u79c1\u6709\u53d8\u91cf\n\n    return function() {\n        count++;    \/\/ \u8bbf\u95ee\u5916\u90e8\u53d8\u91cf\n        return count;\n    };\n}\n\nconst counter = createCounter();\nconsole.log(counter());  \/\/ 1\nconsole.log(counter());  \/\/ 2\nconsole.log(counter());  \/\/ 3\n<\/code><\/pre>\n<p><strong>\u95ed\u5305\u7684\u7528\u9014<\/strong>\uff1a<\/p>\n<ol>\n<li>\u6570\u636e\u79c1\u6709\u5316\uff08\u6a21\u62df\u79c1\u6709\u65b9\u6cd5\uff09<\/li>\n<li>\u51fd\u6570\u5de5\u5382\uff08\u521b\u5efa\u7279\u5b9a\u529f\u80fd\u7684\u51fd\u6570\uff09<\/li>\n<li>\u7ef4\u6301\u72b6\u6001\uff08\u53d8\u91cf\u5728\u51fd\u6570\u8c03\u7528\u95f4\u4fdd\u6301\uff09<\/li>\n<\/ol>\n<h2>\ud83c\udfae \u793a\u4f8b\u4ee3\u7801<\/h2>\n<h3>\u793a\u4f8b1\uff1a\u4f5c\u7528\u57df\u94fe<\/h3>\n<pre><code class=\"language-javascript\">\/\/ \u5168\u5c40\u53d8\u91cf\nlet globalVar = &quot;\u5168\u5c40&quot;;\n\nfunction outerFunction() {\n    \/\/ \u5916\u5c42\u51fd\u6570\u53d8\u91cf\n    let outerVar = &quot;\u5916\u5c42&quot;;\n\n    function innerFunction() {\n        \/\/ \u5185\u5c42\u51fd\u6570\u53d8\u91cf\n        let innerVar = &quot;\u5185\u5c42&quot;;\n\n        console.log(innerVar);  \/\/ \u2705 \u53ef\u4ee5\u8bbf\u95ee\n        console.log(outerVar);  \/\/ \u2705 \u53ef\u4ee5\u8bbf\u95ee\n        console.log(globalVar); \/\/ \u2705 \u53ef\u4ee5\u8bbf\u95ee\n    }\n\n    innerFunction();\n\n    console.log(outerVar);  \/\/ \u2705 \u53ef\u4ee5\u8bbf\u95ee\n    \/\/ console.log(innerVar); \/\/ \u274c \u9519\u8bef\uff1a\u65e0\u6cd5\u8bbf\u95ee\n}\n\nouterFunction();\n\/\/ console.log(outerVar); \/\/ \u274c \u9519\u8bef\uff1a\u65e0\u6cd5\u8bbf\u95ee\nconsole.log(globalVar);   \/\/ \u2705 \u53ef\u4ee5\u8bbf\u95ee\n\n\/*\n\u8f93\u51fa\uff1a\n\u5185\u5c42\n\u5916\u5c42\n\u5168\u5c40\n\u5916\u5c42\n\u5168\u5c40\n*\/\n\n<\/code><\/pre>\n<h3>\u793a\u4f8b2\uff1a\u95ed\u5305\u5b9e\u73b0\u6570\u636e\u79c1\u6709\u5316<\/h3>\n<pre><code class=\"language-javascript\">function createPerson(name) {\n    \/\/ \u79c1\u6709\u53d8\u91cf\n    let age = 0;\n\n    return {\n        getName: function() {\n            return name;\n        },\n        getAge: function() {\n            return age;\n        },\n        setAge: function(newAge) {\n            if (newAge &gt; 0 &amp;&amp; newAge &lt; 150) {\n                age = newAge;\n            } else {\n                console.log(&quot;\u65e0\u6548\u7684\u5e74\u9f84&quot;);\n            }\n        },\n        incrementAge: function() {\n            age++;\n        }\n    };\n}\n\nconst person = createPerson(&quot;\u5f20\u4e09&quot;);\n\nconsole.log(person.getName());  \/\/ \u5f20\u4e09\nconsole.log(person.getAge());   \/\/ 0\n\nperson.setAge(25);\nconsole.log(person.getAge());   \/\/ 25\n\nperson.incrementAge();\nconsole.log(person.getAge());   \/\/ 26\n\n\/\/ \u65e0\u6cd5\u76f4\u63a5\u8bbf\u95eeage\nconsole.log(person.age);        \/\/ undefined\n\/\/ person.age = -10;            \/\/ \u4e0d\u4f1a\u5f71\u54cd\u5185\u90e8age\n\n\/*\n\u8fd9\u6837\u5b9e\u73b0\u4e86\u6570\u636e\u79c1\u6709\u5316\uff0c\u5916\u90e8\u4ee3\u7801\u53ea\u80fd\u901a\u8fc7\u63d0\u4f9b\u7684\u65b9\u6cd5\u8bbf\u95ee\u548c\u4fee\u6539\u6570\u636e\u3002\n*\/\n\n<\/code><\/pre>\n<h3>\u793a\u4f8b3\uff1a\u95ed\u5305\u5b9e\u73b0\u51fd\u6570\u5de5\u5382<\/h3>\n<pre><code class=\"language-javascript\">\/\/ \u51fd\u6570\u5de5\u5382\uff1a\u521b\u5efa\u7279\u5b9a\u529f\u80fd\u7684\u51fd\u6570\nfunction createMultiplier(multiplier) {\n    return function(number) {\n        return number * multiplier;\n    };\n}\n\n\/\/ \u521b\u5efa\u4e0d\u540c\u7684\u4e58\u6cd5\u51fd\u6570\nconst double = createMultiplier(2);\nconst triple = createMultiplier(3);\nconst quadruple = createMultiplier(4);\n\nconsole.log(double(5));    \/\/ 10\nconsole.log(triple(5));    \/\/ 15\nconsole.log(quadruple(5)); \/\/ 20\n\n\/\/ \u5b9e\u9645\u5e94\u7528\uff1a\u6298\u6263\u8ba1\u7b97\u5668\nfunction createDiscountCalculator(discountRate) {\n    return function(price) {\n        return price * (1 - discountRate);\n    };\n}\n\nconst studentDiscount = createDiscountCalculator(0.1);  \/\/ 9\u6298\nconst vipDiscount = createDiscountCalculator(0.2);      \/\/ 8\u6298\n\nconsole.log(studentDiscount(100));  \/\/ 90\nconsole.log(vipDiscount(100));      \/\/ 80\n\n<\/code><\/pre>\n<h3>\u793a\u4f8b4\uff1a\u95ed\u5305\u9677\u9631\uff08\u5faa\u73af\u95ee\u9898\uff09<\/h3>\n<pre><code class=\"language-javascript\">\/\/ \u274c \u9519\u8bef\u793a\u4f8b\uff1a\u5e38\u89c1\u9677\u9631\nfor (var i = 0; i &lt; 3; i++) {\n    setTimeout(function() {\n        console.log(i);  \/\/ \u90fd\u8f93\u51fa 3\n    }, 100);\n}\n\/\/ \u539f\u56e0\uff1avar\u6ca1\u6709\u5757\u7ea7\u4f5c\u7528\u57df\uff0c\u6240\u6709\u51fd\u6570\u5171\u4eab\u540c\u4e00\u4e2ai\n\n\/\/ \u2705 \u89e3\u51b3\u65b9\u6cd51\uff1a\u4f7f\u7528let\nfor (let i = 0; i &lt; 3; i++) {\n    setTimeout(function() {\n        console.log(i);  \/\/ \u8f93\u51fa 0, 1, 2\n    }, 100);\n}\n\n\/\/ \u2705 \u89e3\u51b3\u65b9\u6cd52\uff1a\u4f7f\u7528\u95ed\u5305\nfor (var i = 0; i &lt; 3; i++) {\n    (function(j) {\n        setTimeout(function() {\n            console.log(j);  \/\/ \u8f93\u51fa 0, 1, 2\n        }, 100);\n    })(i);\n}\n\n\/\/ \u2705 \u89e3\u51b3\u65b9\u6cd53\uff1a\u4f7f\u7528IIFE\u8fd4\u56de\u51fd\u6570\nfor (var i = 0; i &lt; 3; i++) {\n    setTimeout(\n        (function(j) {\n            return function() {\n                console.log(j);\n            };\n        })(i),\n        100\n    );\n}\n\n<\/code><\/pre>\n<h2>\u270d\ufe0f \u7ec3\u4e60\u4efb\u52a1<\/h2>\n<h3>\u7ec3\u4e60 1: \u7ec3\u4e601\uff1a\u4f5c\u7528\u57df\u6d4b\u8bd5<\/h3>\n<p>\u5206\u6790\u4ee5\u4e0b\u4ee3\u7801\u7684\u8f93\u51fa\uff1a<\/p>\n<pre><code class=\"language-javascript\">let x = 10;\n\nfunction test1() {\n    let x = 20;\n    console.log(x);\n}\n\nfunction test2() {\n    console.log(x);\n    let x = 30;\n}\n\ntest1();\ntest2();\nconsole.log(x);\n<\/code><\/pre>\n<p><strong>\u95ee\u9898<\/strong>\uff1a<\/p>\n<ol>\n<li>test1() \u8f93\u51fa\u4ec0\u4e48\uff1f<\/li>\n<li>test2() \u4f1a\u62a5\u9519\u5417\uff1f\u4e3a\u4ec0\u4e48\uff1f<\/li>\n<li>\u6700\u540e\u7684 console.log(x) \u8f93\u51fa\u4ec0\u4e48\uff1f<\/li>\n<\/ol>\n<p><strong>\u8981\u6c42<\/strong>\uff1a\u89e3\u91ca\u6bcf\u4e2a\u8f93\u51fa\u7684\u539f\u56e0\uff0c\u8bf4\u660e\u4f5c\u7528\u57df\u89c4\u5219\u3002<\/p>\n<p><strong>\u63d0\u793a<\/strong>: \u6ce8\u610f\u53d8\u91cf\u63d0\u5347\u548c\u6682\u65f6\u6027\u6b7b\u533a\uff08TDZ\uff09<\/p>\n<h3>\u7ec3\u4e60 2: \u7ec3\u4e602\uff1a\u5b9e\u73b0\u8ba1\u6570\u5668<\/h3>\n<p>\u4f7f\u7528\u95ed\u5305\u5b9e\u73b0\u4e00\u4e2a\u8ba1\u6570\u5668\u6a21\u5757\uff0c\u8981\u6c42\uff1a<\/p>\n<ol>\n<li>\n<p>\u63d0\u4f9b\u4ee5\u4e0b\u65b9\u6cd5\uff1a<\/p>\n<ul>\n<li>increment(): \u8ba1\u6570+1<\/li>\n<li>decrement(): \u8ba1\u6570-1<\/li>\n<li>getCount(): \u83b7\u53d6\u5f53\u524d\u8ba1\u6570<\/li>\n<li>reset(): \u91cd\u7f6e\u4e3a0<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u8ba1\u6570\u503c\u4e0d\u80fd\u4e3a\u8d1f\u6570<\/p>\n<\/li>\n<li>\n<p>\u63d0\u4f9b\u591a\u4e2a\u72ec\u7acb\u7684\u8ba1\u6570\u5668\u5b9e\u4f8b<\/p>\n<\/li>\n<\/ol>\n<p><strong>\u793a\u4f8b\u7528\u6cd5<\/strong>\uff1a<\/p>\n<pre><code class=\"language-javascript\">const counter1 = createCounter();\nconst counter2 = createCounter();\n\ncounter1.increment();\ncounter1.increment();\nconsole.log(counter1.getCount());  \/\/ 2\n\ncounter2.increment();\nconsole.log(counter2.getCount());  \/\/ 1\n<\/code><\/pre>\n<p><strong>\u63d0\u793a<\/strong>: \u4f7f\u7528\u95ed\u5305\u4fdd\u62a4count\u53d8\u91cf\uff0c\u8fd4\u56de\u5bf9\u8c61\u5305\u542b\u64cd\u4f5c\u65b9\u6cd5<\/p>\n<h3>\u7ec3\u4e60 3: \u7ec3\u4e603\uff1a\u7f13\u5b58\u51fd\u6570\u7ed3\u679c<\/h3>\n<p>\u4f7f\u7528\u95ed\u5305\u5b9e\u73b0\u4e00\u4e2amemoize\u51fd\u6570\uff0c\u7f13\u5b58\u51fd\u6570\u7684\u8ba1\u7b97\u7ed3\u679c\uff1a<\/p>\n<pre><code class=\"language-javascript\">function memoize(fn) {\n    \/\/ \u4f60\u7684\u4ee3\u7801\n}\n\n\/\/ \u6162\u901f\u51fd\u6570\nfunction slowFunction(n) {\n    console.log(&quot;\u8ba1\u7b97\u4e2d...&quot;);\n    return n * n;\n}\n\nconst memoized = memoize(slowFunction);\n\nconsole.log(memoized(5));  \/\/ \u8f93\u51fa\uff1a\u8ba1\u7b97\u4e2d... 25\nconsole.log(memoized(5));  \/\/ \u76f4\u63a5\u8f93\u51fa\uff1a25\uff08\u4e0d\u8ba1\u7b97\uff09\nconsole.log(memoized(10)); \/\/ \u8f93\u51fa\uff1a\u8ba1\u7b97\u4e2d... 100\nconsole.log(memoized(5));  \/\/ \u76f4\u63a5\u8f93\u51fa\uff1a25\n<\/code><\/pre>\n<p><strong>\u8981\u6c42<\/strong>\uff1a<\/p>\n<ul>\n<li>\u7b2c\u4e00\u6b21\u8c03\u7528\u65f6\u6267\u884c\u5e76\u7f13\u5b58\u7ed3\u679c<\/li>\n<li>\u76f8\u540c\u53c2\u6570\u76f4\u63a5\u8fd4\u56de\u7f13\u5b58\u7ed3\u679c<\/li>\n<li>\u4f7f\u7528\u5bf9\u8c61\u5b58\u50a8\u7f13\u5b58<\/li>\n<\/ul>\n<p><strong>\u63d0\u793a<\/strong>: \u7528\u53c2\u6570\u4f5c\u4e3akey\uff0c\u7ed3\u679c\u4f5c\u4e3avalue\u5b58\u50a8\u5728\u5bf9\u8c61\u4e2d<\/p>\n<h2>\ud83c\udf93 \u4eca\u65e5\u6311\u6218<\/h2>\n<h3>\u4eca\u65e5\u6311\u6218\uff1a\u5b9e\u73b0\u4e8b\u4ef6\u7ba1\u7406\u5668<\/h3>\n<p>\u4f7f\u7528\u95ed\u5305\u5b9e\u73b0\u4e00\u4e2a\u5b8c\u6574\u7684\u4e8b\u4ef6\u7ba1\u7406\u5668\uff08EventEmitter\uff09\uff1a<\/p>\n<p><strong>\u529f\u80fd\u8981\u6c42<\/strong>\uff1a<\/p>\n<ol>\n<li><strong>on(event, callback)<\/strong>\uff1a\u6ce8\u518c\u4e8b\u4ef6\u76d1\u542c\u5668<\/li>\n<\/ol>\n<pre><code class=\"language-javascript\">emitter.on('click', function(data) {\n    console.log('\u70b9\u51fb\u4e8b\u4ef6:', data);\n});\n<\/code><\/pre>\n<ol start=\"2\">\n<li><strong>emit(event, data)<\/strong>\uff1a\u89e6\u53d1\u4e8b\u4ef6<\/li>\n<\/ol>\n<pre><code class=\"language-javascript\">emitter.emit('click', {x: 100, y: 200});\n<\/code><\/pre>\n<ol start=\"3\">\n<li><strong>off(event, callback)<\/strong>\uff1a\u79fb\u9664\u4e8b\u4ef6\u76d1\u542c\u5668<\/li>\n<\/ol>\n<pre><code class=\"language-javascript\">emitter.off('click', handler);\n<\/code><\/pre>\n<ol start=\"4\">\n<li><strong>once(event, callback)<\/strong>\uff1a\u53ea\u89e6\u53d1\u4e00\u6b21\u7684\u76d1\u542c\u5668<\/li>\n<\/ol>\n<pre><code class=\"language-javascript\">emitter.once('init', function() {\n    console.log('\u53ea\u6267\u884c\u4e00\u6b21');\n});\n<\/code><\/pre>\n<p><strong>\u6280\u672f\u8981\u6c42<\/strong>\uff1a<\/p>\n<ul>\n<li>\u4f7f\u7528\u95ed\u5305\u4fdd\u62a4events\u5bf9\u8c61<\/li>\n<li>\u652f\u6301\u540c\u4e00\u4e8b\u4ef6\u7684\u591a\u4e2a\u76d1\u542c\u5668<\/li>\n<li>once\u76d1\u542c\u5668\u89e6\u53d1\u540e\u81ea\u52a8\u79fb\u9664<\/li>\n<li>\u79fb\u9664\u4e0d\u5b58\u5728\u7684\u76d1\u542c\u5668\u4e0d\u62a5\u9519<\/li>\n<\/ul>\n<p><strong>\u6d4b\u8bd5\u4ee3\u7801<\/strong>\uff1a<\/p>\n<pre><code class=\"language-javascript\">const emitter = createEventEmitter();\n\nlet count = 0;\nemitter.on('test', () =&gt; count++);\nemitter.on('test', () =&gt; count++);\nemitter.once('test', () =&gt; count += 10);\n\nemitter.emit('test');  \/\/ count = 12 (2 + 10)\nemitter.emit('test');  \/\/ count = 14 (2\uff0conce\u5df2\u79fb\u9664)\n<\/code><\/pre>\n<p><strong>\u6269\u5c55\u529f\u80fd<\/strong>\uff08\u53ef\u9009\uff09\uff1a<\/p>\n<ul>\n<li>\u652f\u6301\u901a\u914d\u7b26\u76d1\u542c\u6240\u6709\u4e8b\u4ef6<\/li>\n<li>\u6dfb\u52a0removeAllListeners(event)\u65b9\u6cd5<\/li>\n<li>\u6dfb\u52a0listenerCount(event)\u65b9\u6cd5<\/li>\n<\/ul>\n<p><strong>\u63d0\u793a<\/strong>: \u4f7f\u7528\u5bf9\u8c61\u5b58\u50a8\u4e8b\u4ef6\uff0ckey\u662f\u4e8b\u4ef6\u540d\uff0cvalue\u662f\u56de\u8c03\u6570\u7ec4<\/p>\n<h2>\ud83d\udca1 \u5e38\u89c1\u95ee\u9898<\/h2>\n<h3>Q1: \u4ec0\u4e48\u65f6\u5019\u4f7f\u7528\u95ed\u5305\uff1f<\/h3>\n<p><strong>A<\/strong>: \u95ed\u5305\u7684\u5178\u578b\u4f7f\u7528\u573a\u666f\uff1a<\/p>\n<p><strong>1. \u6570\u636e\u79c1\u6709\u5316<\/strong>\uff1a<\/p>\n<pre><code class=\"language-javascript\">function createBankAccount() {\n    let balance = 0;\n    return {\n        deposit: (amount) =&gt; balance += amount,\n        getBalance: () =&gt; balance\n    };\n}\n<\/code><\/pre>\n<p><strong>2. \u51fd\u6570\u5de5\u5382<\/strong>\uff1a<\/p>\n<pre><code class=\"language-javascript\">function createGreeter(greeting) {\n    return function(name) {\n        console.log(`${greeting}, ${name}!`);\n    };\n}\n<\/code><\/pre>\n<p><strong>3. \u7ef4\u6301\u72b6\u6001<\/strong>\uff1a<\/p>\n<pre><code class=\"language-javascript\">function createCounter() {\n    let count = 0;\n    return () =&gt; ++count;\n}\n<\/code><\/pre>\n<p><strong>\u4ec0\u4e48\u65f6\u5019\u907f\u514d<\/strong>\uff1a<\/p>\n<ul>\n<li>\u4e0d\u9700\u8981\u4fdd\u6301\u72b6\u6001\u65f6\uff0c\u4e0d\u8981\u7528\u95ed\u5305<\/li>\n<li>\u6ce8\u610f\u5185\u5b58\u5360\u7528\uff0c\u95ed\u5305\u4f1a\u4e00\u76f4\u4fdd\u6301\u4f5c\u7528\u57df<\/li>\n<\/ul>\n<h3>Q2: \u95ed\u5305\u4f1a\u5bfc\u81f4\u5185\u5b58\u6cc4\u6f0f\u5417\uff1f<\/h3>\n<p><strong>A<\/strong>: \u95ed\u5305\u672c\u8eab\u4e0d\u4f1a\u5bfc\u81f4\u5185\u5b58\u6cc4\u6f0f\uff0c\u4f46\u4e0d\u5c0f\u5fc3\u4f7f\u7528\u4f1a\u5360\u7528\u8fc7\u591a\u5185\u5b58\u3002<\/p>\n<p><strong>\u95ee\u9898\u793a\u4f8b<\/strong>\uff1a<\/p>\n<pre><code class=\"language-javascript\">function createElements() {\n    let largeData = new Array(1000000);\n    \n    document.getElementById('btn')\n        .addEventListener('click', function() {\n            console.log('clicked');\n            \/\/ largeData\u88ab\u95ed\u5305\u5f15\u7528\uff0c\u4e0d\u4f1a\u88ab\u56de\u6536\n        });\n}\n<\/code><\/pre>\n<p><strong>\u89e3\u51b3\u65b9\u6848<\/strong>\uff1a<\/p>\n<pre><code class=\"language-javascript\">function createElements() {\n    let largeData = new Array(1000000);\n    \n    function handleClick() {\n        console.log('clicked');\n        \/\/ \u4e0d\u5f15\u7528largeData\n    }\n    \n    document.getElementById('btn')\n        .addEventListener('click', handleClick);\n    \n    \/\/ \u4f7f\u7528\u5b8c\u540e\u53ef\u4ee5\u91ca\u653e\n    largeData = null;\n}\n<\/code><\/pre>\n<p><strong>\u6700\u4f73\u5b9e\u8df5<\/strong>\uff1a<\/p>\n<ul>\n<li>\u95ed\u5305\u4e2d\u53ea\u5f15\u7528\u9700\u8981\u7684\u6570\u636e<\/li>\n<li>\u4e0d\u518d\u4f7f\u7528\u65f6\u8bbe\u7f6e\u4e3anull<\/li>\n<li>\u79fb\u9664\u4e8b\u4ef6\u76d1\u542c\u5668\uff1aremoveEventListener<\/li>\n<\/ul>\n<h3>Q3: let\u3001const\u3001var\u6709\u4ec0\u4e48\u533a\u522b\uff1f<\/h3>\n<p><strong>A<\/strong>: <strong>\u4e09\u5927\u533a\u522b<\/strong>\uff1a<\/p>\n<p><strong>1. \u4f5c\u7528\u57df<\/strong>\uff1a<\/p>\n<ul>\n<li>var: \u51fd\u6570\u4f5c\u7528\u57df<\/li>\n<li>let\/const: \u5757\u7ea7\u4f5c\u7528\u57df<\/li>\n<\/ul>\n<pre><code class=\"language-javascript\">if (true) {\n    var a = 1;\n    let b = 2;\n}\nconsole.log(a);  \/\/ 1\nconsole.log(b);  \/\/ ReferenceError\n<\/code><\/pre>\n<p><strong>2. \u53d8\u91cf\u63d0\u5347<\/strong>\uff1a<\/p>\n<ul>\n<li>var: \u63d0\u5347\u5e76\u521d\u59cb\u5316\u4e3aundefined<\/li>\n<li>let\/const: \u63d0\u5347\u4f46\u4e0d\u521d\u59cb\u5316\uff08TDZ\uff09<\/li>\n<\/ul>\n<pre><code class=\"language-javascript\">console.log(a);  \/\/ undefined\nvar a = 1;\n\nconsole.log(b);  \/\/ ReferenceError\nlet b = 2;\n<\/code><\/pre>\n<p><strong>3. \u91cd\u590d\u58f0\u660e<\/strong>\uff1a<\/p>\n<ul>\n<li>var: \u5141\u8bb8\u91cd\u590d\u58f0\u660e<\/li>\n<li>let\/const: \u4e0d\u5141\u8bb8<\/li>\n<\/ul>\n<pre><code class=\"language-javascript\">var a = 1;\nvar a = 2;  \/\/ OK\n\nlet b = 1;\nlet b = 2;  \/\/ SyntaxError\n<\/code><\/pre>\n<p><strong>\u9009\u62e9\u5efa\u8bae<\/strong>\uff1a<\/p>\n<ul>\n<li>\u9ed8\u8ba4\u4f7f\u7528const\uff08\u4e0d\u4f1a\u88ab\u91cd\u65b0\u8d4b\u503c\uff09<\/li>\n<li>\u9700\u8981\u91cd\u65b0\u8d4b\u503c\u65f6\u4f7f\u7528let<\/li>\n<li>\u907f\u514d\u4f7f\u7528var<\/li>\n<\/ul>\n<h3>Q4: \u5982\u4f55\u8c03\u8bd5\u95ed\u5305\u95ee\u9898\uff1f<\/h3>\n<p><strong>A<\/strong>: <strong>\u8c03\u8bd5\u95ed\u5305\u7684\u6280\u5de7<\/strong>\uff1a<\/p>\n<p><strong>1. \u4f7f\u7528console.log\u67e5\u770b\u95ed\u5305\u53d8\u91cf<\/strong>\uff1a<\/p>\n<pre><code class=\"language-javascript\">function outer() {\n    let count = 0;\n    return function() {\n        console.log('count:', count);  \/\/ \u67e5\u770b\u503c\n        count++;\n    };\n}\n<\/code><\/pre>\n<p><strong>2. \u6d4f\u89c8\u5668\u5f00\u53d1\u8005\u5de5\u5177<\/strong>\uff1a<\/p>\n<ul>\n<li>\u5728\u95ed\u5305\u51fd\u6570\u5185\u8bbe\u7f6e\u65ad\u70b9<\/li>\n<li>\u67e5\u770bScope\u9762\u677f\u7684Closure\u90e8\u5206<\/li>\n<li>\u53ef\u4ee5\u770b\u5230\u6240\u6709\u95ed\u5305\u53d8\u91cf<\/li>\n<\/ul>\n<p><strong>3. \u68c0\u67e5\u610f\u5916\u7684\u95ed\u5305<\/strong>\uff1a<\/p>\n<pre><code class=\"language-javascript\">\/\/ \u68c0\u67e5\u51fd\u6570\u957f\u5ea6\nfunction create() {\n    let data = 'large data';\n    return function() {\n        console.log('hello');\n        \/\/ data\u88ab\u610f\u5916\u5f15\u7528\n    };\n}\n\n\/\/ \u89e3\u51b3\uff1a\u660e\u786e\u4e0d\u5f15\u7528\nfunction create() {\n    let data = 'large data';\n    let result = function() {\n        console.log('hello');\n    };\n    data = null;  \/\/ \u91ca\u653e\u5f15\u7528\n    return result;\n}\n<\/code><\/pre>\n<p><strong>4. \u5185\u5b58\u5206\u6790<\/strong>\uff1a<\/p>\n<ul>\n<li>\u4f7f\u7528Chrome Memory\u9762\u677f<\/li>\n<li>\u62cd\u6444\u5806\u5feb\u7167<\/li>\n<li>\u67e5\u770b\u5206\u79bb\u7684DOM\u6811\u548c\u95ed\u5305<\/li>\n<\/ul>\n<hr>\n<p><strong>\u5b66\u4e60\u65f6\u95f4<\/strong>: 2026-03-09 22:22<br \/>\n<strong>\u96be\u5ea6<\/strong>: \u2b50\u2b50\u2b50\u2b50\u2606<br \/>\n<strong>\u9884\u8ba1\u7528\u65f6<\/strong>: 2-3 \u5c0f\u65f6<br \/>\n<strong>\u5173\u952e\u8bcd<\/strong>: \u4f5c\u7528\u57df\u4e0e\u95ed\u5305<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Day 8: \u4f5c\u7528\u57df\u4e0e\u95ed\u5305 \ud83c\udfaf \u5b66\u4e60\u76ee\u6807 \u7406\u89e3 \u4f5c\u7528\u57df\u4e0e\u95ed\u5305 \u7684\u6838\u5fc3\u6982\u5ff5\u548c\u5de5\u4f5c\u539f\u7406 \u638c\u63e1\u76f8\u5173\u8bed\u6cd5\u548c\u6700\u4f73\u5b9e\u8df5 \u5b66 &#8230; <a title=\"Day-8-\u4f5c\u7528\u57df\u4e0e\u95ed\u5305\" class=\"read-more\" href=\"https:\/\/www.resilence.cn\/?p=235\" aria-label=\"\u7ee7\u7eed\u9605\u8bfbDay-8-\u4f5c\u7528\u57df\u4e0e\u95ed\u5305\">\u9605\u8bfb\u66f4\u591a<\/a><\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-235","post","type-post","status-publish","format-standard","hentry","category-studycoding"],"_links":{"self":[{"href":"https:\/\/www.resilence.cn\/index.php?rest_route=\/wp\/v2\/posts\/235","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.resilence.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.resilence.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.resilence.cn\/index.php?rest_route=\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/www.resilence.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=235"}],"version-history":[{"count":1,"href":"https:\/\/www.resilence.cn\/index.php?rest_route=\/wp\/v2\/posts\/235\/revisions"}],"predecessor-version":[{"id":236,"href":"https:\/\/www.resilence.cn\/index.php?rest_route=\/wp\/v2\/posts\/235\/revisions\/236"}],"wp:attachment":[{"href":"https:\/\/www.resilence.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=235"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.resilence.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=235"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.resilence.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=235"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}