{"id":155,"date":"2022-08-20T11:33:15","date_gmt":"2022-08-20T03:33:15","guid":{"rendered":"http:\/\/weboob.top\/blog\/?p=155"},"modified":"2022-08-20T11:33:15","modified_gmt":"2022-08-20T03:33:15","slug":"javascript%e4%b8%ad%e7%9a%84%e9%97%ad%e5%8c%85","status":"publish","type":"post","link":"http:\/\/blog.weboob.top\/?p=155","title":{"rendered":"Javascript\u4e2d\u7684\u95ed\u5305"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">\u5148\u5bfc\u77e5\u8bc6\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>JavaScript\u4e2d\u4f5c\u7528\u57df\u6709\u4e24\u5927\u7c7b\uff1a<strong>\u5168\u5c40\u4f5c\u7528\u57df<\/strong>\u548c<strong>\u5c40\u90e8\u4f5c\u7528\u57df<\/strong><\/li><li>JavaScript\u4e2d\u53d8\u91cf\u6709\uff1a<strong>\u5168\u5c40\u53d8\u91cf<\/strong>\u548c<strong>\u5c40\u90e8\u53d8\u91cf<\/strong><\/li><li>JavaScript\u4e2d\u6ca1\u6709\u547d\u540d\u7a7a\u95f4\u7684\u6982\u5ff5<\/li><li>JavaScript\u4e2d\u51fd\u6570\u5185\u53ef\u4ee5\u5d4c\u5957\u5b9a\u4e49\u51fd\u6570<\/li><li>\u5728\u51fd\u6570\u5c40\u90e8\u4f5c\u7528\u57df\u4e2d\u53ef\u4ee5\u8bbf\u95ee\u5168\u5c40\u53d8\u91cf\uff0c\u53cd\u4e4b\u4e0d\u884c\u3002<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Question\uff1a\u6211\u4eec\u9700\u8981\u5728\u5168\u5c40\u4f5c\u7528\u57df\u4e2d\u8bbf\u95ee\u5c40\u90e8\u4f5c\u7528\u57df\u4e2d\u7684\u5c40\u90e8\u53d8\u91cf\uff0c\u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u6709\u4e86\u95ed\u5305\u7684\u6982\u5ff5\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"%E4%B8%80%E5%AE%9A%E4%B9%89\">\u4e00\u3001\u5b9a\u4e49<\/h3>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>\u300ajavascript\u9ad8\u7ea7\u7a0b\u5e8f\u8bbe\u8ba1\u300b\uff1a\u95ed\u5305\u662f\u6307\u6709\u6743\u9650\u8bbf\u95ee\u4e00\u4e2a\u51fd\u6570\u4f5c\u7528\u57df\u4e2d\u5c40\u90e8\u53d8\u91cf\u7684\u51fd\u6570\u3002<\/p><\/blockquote>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"%E4%BA%8C%E5%B1%95%E7%A4%BA\">\u4e8c\u3001\u5c55\u793a<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5047\u8bbe\u6211\u4eec\u9700\u8981\u4e00\u4e2a\u8ba1\u6570\u5668\u53d8\u91cfcount\uff0c\u7528\u6765\u8ba1\u6570\u3002\u8fd9\u65f6\u6211\u4eec\u53ef\u4ee5\u8fd9\u6837\u53bb\u5199\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><em>\/**\n * \u76f4\u63a5\u5b9a\u4e49\u5168\u5c40\u53d8\u91cf\u7528\u6765\u8ba1\u6570\n *\/<\/em>\nvar count = 0;\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f46\u662f\u8fd9\u6837\u4e00\u6765\u4efb\u4f55\uff0c\u4efb\u4f55\u5730\u65b9\u90fd\u53ef\u4ee5\u8bbf\u95ee\u8fd9\u4e2a\u53d8\u91cf\uff0c\u800c\u4e14\u4f1a\u6709\u88ab\u91cd\u65b0\u8d4b\u503c\u7684\u5371\u9669\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u6240\u4ee5\u6211\u4eec\u53ef\u4ee5\u8fd9\u6837\u53bb\u5199\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><em>\/**\n * \u5c06\u8ba1\u6570\u53d8\u91cf\u653e\u5230\u51fd\u6570\u5185\u90e8\n *\/<\/em>\nfunction count(){\n    var count = 0;\n    return count += 1;\n}\n\ncount();    <em>\/\/ 1<\/em>\ncount();    <em>\/\/ 1<\/em>\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f46\u662f\u8fd9\u6837\u5c31\u4f1a\u4e00\u76f4\u8f93\u51fa1\uff0c\u4ee5\u4e3a\u6bcf\u6b21\u8c03\u7528\u90fd\u4f1a\u91cd\u65b0\u5b9a\u4e49\u4e00\u4e2acount\u5e76\u521d\u59cb\u5316\u8fd9\u4e2a\u53d8\u91cf\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u90a3\u518d\u6539\u9020\u4e00\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><em>\/**\n * \u6539\u9020\u7248\u7684\u8ba1\u6570\u5668\u5b9e\u73b0\n *\/<\/em>\nfunction counter(){\n    let count = 0;\n    function plus(){\n        count += 1;\n    }\n    plus();\n    return count;\n}\nconsole.log(counter());     <em>\/\/ 1<\/em>\nconsole.log(counter());     <em>\/\/ 1<\/em>\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u6211\u4eec\u53d1\u73b0\u5185\u5d4c\u51fd\u6570\u53ef\u4ee5\u8bbf\u95ee\u5916\u5c42\u51fd\u6570\u7684\u53d8\u91cf\uff0c\u8fd9\u6837\u6211\u4eec\u53ea\u8981\u4fdd\u8bc1\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>\u5916\u5c42\u51fd\u6570\u65e0\u6cd5\u8bbf\u95ee<\/li><li>\u5185\u90e8\u53d8\u91cf\u53ea\u521d\u59cb\u5316\u4e00\u6b21<\/li><li>\u5916\u5c42\u51fd\u6570\u66b4\u9732\u4e00\u4e2a\u63a5\u53e3\uff0c\u8be5\u63a5\u53e3\u53ef\u4ee5\u64cd\u4f5c\u5176\u5185\u90e8\u53d8\u91cf<\/li><\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code><em>\/**\n * \u8ba1\u6570\u5668\u7684\u95ed\u5305\u5b9e\u73b0\n * \u901a\u8fc7\u7acb\u5373\u6267\u884c\u7684\u533f\u540d\u51fd\u6570 + \u5185\u90e8\u51fd\u6570\n *\/<\/em>\nlet counter2 = (function(){\n    let count = 0;\n    return function(){ return count += 1; }\n})();\n\nconsole.log(counter2());  <em>\/\/ 1<\/em>\nconsole.log(counter2());  <em>\/\/ 2<\/em>\nconsole.log(counter2());  <em>\/\/ 3<\/em>\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u6837\u7acb\u5373\u6267\u884c\u51fd\u6570\u53ea\u6267\u884c\u4e00\u6b21\uff0c\u540c\u65f6\u53c8\u53ef\u4ee5\u8fd4\u56de\u4e00\u4e2a\u51fd\u6570\u7528\u6765\u64cd\u4f5c\u5916\u5c42\u533f\u540d\u51fd\u6570\u7684\u5c40\u90e8\u53d8\u91cf\uff0c\u8fd9\u6837\u5c31\u5f62\u6210\u4e86\u4e00\u4e2a&nbsp;<strong>\u95ed\u5305<\/strong>\u3002<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>\u95ed\u5305\u662f\u4e00\u79cd\u79c1\u6709\u53d8\u91cf\u4fdd\u62a4\u673a\u5236\uff0c\u5728\u51fd\u6570\u6267\u884c\u65f6\u5f62\u6210\u79c1\u6709\u4f5c\u7528\u57df\uff0c\u4fdd\u62a4\u91cc\u9762\u7684\u79c1\u6709\u53d8\u91cf\u4e0d\u53d7\u5916\u63a5\u73af\u5883\u7684\u5f71\u54cd\u3002<br \/>\u76f4\u89c2\u7684\u8bf4\u5c31\u662f\u5f62\u6210\u4e00\u4e2a\u4e0d\u9500\u6bc1\u7684\u6808\u73af\u5883\u3002<\/p><\/blockquote>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"%E7%BC%BA%E7%82%B9\">\u7f3a\u70b9<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u56e0\u4e3a\u65e0\u6cd5\u9500\u6bc1\uff0c\u6240\u4ee5\u53ef\u80fd\u9020\u6210\u5185\u5b58\u6cc4\u9732\uff01\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5148\u5bfc\u77e5\u8bc6\uff1a JavaScript\u4e2d\u4f5c\u7528\u57df\u6709\u4e24\u5927\u7c7b\uff1a\u5168\u5c40\u4f5c\u7528\u57df\u548c\u5c40\u90e8\u4f5c\u7528\u57df JavaScript\u4e2d\u53d8\u91cf\u6709\uff1a\u5168\u5c40\u53d8 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10],"tags":[22,30],"class_list":["post-155","post","type-post","status-publish","format-standard","hentry","category-js","tag-javascript","tag-30"],"_links":{"self":[{"href":"http:\/\/blog.weboob.top\/index.php?rest_route=\/wp\/v2\/posts\/155","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/blog.weboob.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/blog.weboob.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/blog.weboob.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/blog.weboob.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=155"}],"version-history":[{"count":0,"href":"http:\/\/blog.weboob.top\/index.php?rest_route=\/wp\/v2\/posts\/155\/revisions"}],"wp:attachment":[{"href":"http:\/\/blog.weboob.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=155"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/blog.weboob.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=155"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/blog.weboob.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=155"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}