{"id":3337,"date":"2024-11-10T09:28:58","date_gmt":"2024-11-10T01:28:58","guid":{"rendered":"https:\/\/fwq.ai\/blog\/3337\/"},"modified":"2024-11-10T09:28:58","modified_gmt":"2024-11-10T01:28:58","slug":"js-%e9%97%ad%e5%8c%85%e5%a6%82%e4%bd%95%e8%b0%83%e7%94%a8","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/3337\/","title":{"rendered":"js \u95ed\u5305\u5982\u4f55\u8c03\u7528"},"content":{"rendered":"<blockquote><p>\n  \u8c03\u7528 javascript \u95ed\u5305\u7684\u6b65\u9aa4\uff1a\u58f0\u660e\u4e00\u4e2a\u521b\u5efa\u95ed\u5305\u7684\u5916\u90e8\u51fd\u6570\u3002\u5728\u5916\u90e8\u51fd\u6570\u4e2d\u5b9a\u4e49\u9700\u8981\u8bbf\u95ee\u7684\u53d8\u91cf\u548c\u51fd\u6570\u3002\u8fd4\u56de\u4e00\u4e2a\u5185\u90e8\u51fd\u6570\u4f5c\u4e3a\u95ed\u5305\u3002\u5728\u9700\u8981\u4f7f\u7528\u95ed\u5305\u7684\u5730\u65b9\u8c03\u7528\u5185\u90e8\u51fd\u6570\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/08\/2024110810004754305.jpg\" class=\"aligncenter\" title=\"js \u95ed\u5305\u5982\u4f55\u8c03\u7528\u63d2\u56fe\" alt=\"js \u95ed\u5305\u5982\u4f55\u8c03\u7528\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u8c03\u7528 JavaScript \u95ed\u5305<\/strong><\/p>\n<p><strong>\u4ec0\u4e48\u662f\u95ed\u5305\uff1f<\/strong><br \/>\u95ed\u5305\u662f\u5728 JavaScript \u4e2d\u521b\u5efa\u7684\u51fd\u6570\uff0c\u5b83\u53ef\u4ee5\u8bbf\u95ee\u5176\u521b\u5efa\u8303\u56f4\u4e4b\u5916\u7684\u53d8\u91cf\u548c\u51fd\u6570\uff0c\u5373\u4f7f\u5176\u5916\u5c42\u51fd\u6570\u5df2\u7ecf\u8fd4\u56de\u3002<\/p>\n<p><strong>\u8c03\u7528\u95ed\u5305\u7684\u6b65\u9aa4\uff1a<\/strong><\/p>\n<ol>\n<li><strong>\u58f0\u660e\u4e00\u4e2a\u521b\u5efa\u95ed\u5305\u7684\u5916\u90e8\u51fd\u6570\u3002<\/strong><\/li>\n<li><strong>\u5728\u5916\u90e8\u51fd\u6570\u4e2d\u5b9a\u4e49\u9700\u8981\u8bbf\u95ee\u7684\u53d8\u91cf\u548c\u51fd\u6570\u3002<\/strong><\/li>\n<li><strong>\u8fd4\u56de\u4e00\u4e2a\u5185\u90e8\u51fd\u6570\u4f5c\u4e3a\u95ed\u5305\u3002<\/strong><\/li>\n<li><strong>\u5728\u9700\u8981\u4f7f\u7528\u95ed\u5305\u7684\u5730\u65b9\u8c03\u7528\u5185\u90e8\u51fd\u6570\u3002<\/strong><\/li>\n<\/ol>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>function createCounter() {\n  let count = 0;  \/\/ \u5916\u90e8\u53d8\u91cf\n  \n  \/\/ \u8fd4\u56de\u4e00\u4e2a\u5185\u90e8\u51fd\u6570\u4f5c\u4e3a\u95ed\u5305\n  return function() {\n    return count++;  \/\/ \u8bbf\u95ee\u5916\u90e8\u53d8\u91cf\u5e76\u9012\u589e\n  };\n}\n\nconst counter = createCounter();\nconsole.log(counter());  \/\/ 0\nconsole.log(counter());  \/\/ 1<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0ccreateCounter \u51fd\u6570\u521b\u5efa\u4e86\u4e00\u4e2a\u95ed\u5305\uff0c\u8be5\u95ed\u5305\u5305\u542b\u4e00\u4e2a\u540d\u4e3a count \u7684\u53d8\u91cf\u3002\u5185\u90e8\u51fd\u6570\u4f5c\u4e3a\u95ed\u5305\u88ab\u8fd4\u56de\uff0c\u53ef\u4ee5\u8bbf\u95ee count \u53d8\u91cf\u5e76\u5c06\u5176\u9012\u589e\u3002<\/p>\n<p><strong>\u6ce8\u610f\uff1a<\/strong><\/p>\n<ul>\n<li>\u95ed\u5305\u53ef\u4ee5\u591a\u6b21\u8c03\u7528\uff0c\u5e76\u4e14\u6bcf\u6b21\u8c03\u7528\u90fd\u4f1a\u8bbf\u95ee\u5176\u521b\u5efa\u8303\u56f4\u4e2d\u7684\u76f8\u540c\u53d8\u91cf\u3002<\/li>\n<li>\u95ed\u5305\u53ef\u4ee5\u6355\u83b7\u5bf9\u5b83\u4eec\u7684\u7236\u51fd\u6570\u4e2d\u4efb\u4f55\u53d8\u91cf\u7684\u5f15\u7528\uff0c\u5305\u62ec\u53c2\u6570\u3001\u5c40\u90e8\u53d8\u91cf\u548c\u5168\u5c40\u53d8\u91cf\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662fjs \u95ed\u5305\u5982\u4f55\u8c03\u7528\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u8c03\u7528 javascript \u95ed\u5305\u7684\u6b65\u9aa4\uff1a\u58f0\u660e\u4e00\u4e2a\u521b\u5efa\u95ed\u5305\u7684\u5916\u90e8\u51fd\u6570\u3002\u5728\u5916\u90e8\u51fd\u6570\u4e2d\u5b9a\u4e49\u9700\u8981\u8bbf\u95ee\u7684\u53d8\u91cf\u548c\u51fd\u6570\u3002\u8fd4\u56de\u4e00\u4e2a\u5185\u90e8\u51fd\u6570\u4f5c\u4e3a\u95ed\u5305\u3002\u5728\u9700\u8981\u4f7f\u7528\u95ed\u5305\u7684\u5730\u65b9\u8c03\u7528\u5185\u90e8\u51fd\u6570\u3002 \u5982\u4f55\u8c03\u7528 JavaScript \u95ed\u5305 \u4ec0\u4e48\u662f\u95ed\u5305\uff1f\u95ed\u5305\u662f\u5728 JavaScript \u4e2d\u521b\u5efa\u7684\u51fd\u6570\uff0c\u5b83\u53ef\u4ee5\u8bbf\u95ee\u5176\u521b\u5efa\u8303\u56f4\u4e4b\u5916\u7684\u53d8\u91cf\u548c\u51fd\u6570\uff0c\u5373\u4f7f\u5176\u5916\u5c42\u51fd\u6570\u5df2\u7ecf\u8fd4\u56de\u3002 \u8c03\u7528\u95ed\u5305\u7684\u6b65\u9aa4\uff1a \u58f0\u660e\u4e00\u4e2a\u521b\u5efa\u95ed\u5305\u7684\u5916\u90e8\u51fd\u6570\u3002 \u5728\u5916\u90e8\u51fd\u6570\u4e2d\u5b9a\u4e49\u9700\u8981\u8bbf\u95ee\u7684\u53d8\u91cf\u548c\u51fd\u6570\u3002 \u8fd4\u56de\u4e00\u4e2a\u5185\u90e8\u51fd\u6570\u4f5c\u4e3a\u95ed\u5305\u3002 \u5728\u9700\u8981\u4f7f\u7528\u95ed\u5305\u7684\u5730\u65b9\u8c03\u7528\u5185\u90e8\u51fd\u6570\u3002 \u793a\u4f8b\uff1a function createCounter() { let count = 0; \/\/ \u5916\u90e8\u53d8\u91cf \/\/ \u8fd4\u56de\u4e00\u4e2a\u5185\u90e8\u51fd\u6570\u4f5c\u4e3a\u95ed\u5305 return function() { return count++; \/\/ \u8bbf\u95ee\u5916\u90e8\u53d8\u91cf\u5e76\u9012\u589e }; } const counter = createCounter(); console.log(counter()); \/\/ 0 console.log(counter()); \/\/ 1 \u767b\u5f55\u540e\u590d\u5236 \u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0ccreateCounter \u51fd\u6570\u521b\u5efa\u4e86\u4e00\u4e2a\u95ed\u5305\uff0c\u8be5\u95ed\u5305\u5305\u542b\u4e00\u4e2a\u540d\u4e3a count \u7684\u53d8\u91cf\u3002\u5185\u90e8\u51fd\u6570\u4f5c\u4e3a\u95ed\u5305\u88ab\u8fd4\u56de\uff0c\u53ef\u4ee5\u8bbf\u95ee count \u53d8\u91cf\u5e76\u5c06\u5176\u9012\u589e\u3002 \u6ce8\u610f\uff1a \u95ed\u5305\u53ef\u4ee5\u591a\u6b21\u8c03\u7528\uff0c\u5e76\u4e14\u6bcf\u6b21\u8c03\u7528\u90fd\u4f1a\u8bbf\u95ee\u5176\u521b\u5efa\u8303\u56f4\u4e2d\u7684\u76f8\u540c\u53d8\u91cf\u3002 \u95ed\u5305\u53ef\u4ee5\u6355\u83b7\u5bf9\u5b83\u4eec\u7684\u7236\u51fd\u6570\u4e2d\u4efb\u4f55\u53d8\u91cf\u7684\u5f15\u7528\uff0c\u5305\u62ec\u53c2\u6570\u3001\u5c40\u90e8\u53d8\u91cf\u548c\u5168\u5c40\u53d8\u91cf\u3002 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-3337","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/3337","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=3337"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/3337\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=3337"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=3337"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=3337"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}