{"id":2635,"date":"2024-11-10T11:42:23","date_gmt":"2024-11-10T03:42:23","guid":{"rendered":"https:\/\/fwq.ai\/blog\/2635\/"},"modified":"2024-11-10T11:42:23","modified_gmt":"2024-11-10T03:42:23","slug":"javascript-%e4%b8%ad%e7%9a%84%e8%ae%b0%e5%bf%86","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/2635\/","title":{"rendered":"JavaScript \u4e2d\u7684\u8bb0\u5fc6"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173081777371482.jpg\" class=\"aligncenter\" title=\"JavaScript \u4e2d\u7684\u8bb0\u5fc6\u63d2\u56fe\" alt=\"JavaScript \u4e2d\u7684\u8bb0\u5fc6\u63d2\u56fe\" \/><\/p>\n<p>\u8bb0\u5fc6\u5316\uff08\u6216\u201c\u8bb0\u5fc6\u5316\u201d\uff09\u662f\u4e00\u79cd\u4f18\u5316\u6280\u672f\uff0c\u53ef\u4ee5\u7f13\u5b58\u7e41\u91cd\u6216\u8017\u65f6\u7684\u51fd\u6570\u7684\u7ed3\u679c\uff0c\u4ee5\u4fbf\u5c06\u6765\u4f7f\u7528\u76f8\u540c\u53c2\u6570\u7684\u8c03\u7528\u66f4\u5feb\u3002\u8bb0\u5fc6\u5728\u7eaf\u51fd\u6570\u4e2d\u7279\u522b\u6709\u7528\uff0c\u56e0\u4e3a\u540c\u4e00\u7ec4\u8f93\u5165\u603b\u662f\u4ea7\u751f\u76f8\u540c\u7684\u7ed3\u679c\u3002<\/p>\n<p><strong>\u8bb0\u5fc6\u662f\u5982\u4f55\u8fd0\u4f5c\u7684\uff1f<\/strong><\/p>\n<p>\u5f53\u7b2c\u4e00\u6b21\u4f7f\u7528\u67d0\u4e9b\u53c2\u6570\u8c03\u7528\u8bb0\u5fc6\u7684\u51fd\u6570\u65f6\uff0c\u5b83\u4f1a\u6267\u884c\u8ba1\u7b97\u5e76\u5c06\u7ed3\u679c\u5b58\u50a8\u5728\u7f13\u5b58\u4e2d\u3002\u540e\u7eed\u8c03\u7528\u76f8\u540c\u53c2\u6570\u65f6\uff0c\u51fd\u6570\u4f1a\u76f4\u63a5\u4ece\u7f13\u5b58\u4e2d\u8fd4\u56de\u7ed3\u679c\uff0c\u907f\u514d\u91cd\u590d\u8ba1\u7b97\u3002<\/p>\n<p>\u57fa\u672c\u8bb0\u5fc6\u5b9e\u73b0<\/p>\n<pre>function memoize(fn) {\n  const cache = {};\n\n  return function(...args) {\n    const key = json.stringify(args); \/\/ cria uma chave \u00fanica para os argumentos\n\n    if (cache[key]) {\n      console.log(\"resultado em cache\");\n      return cache[key]; \/\/ retorna o valor armazenado no cache\n    }\n\n    const result = fn(...args); \/\/ calcula o valor\n    cache[key] = result; \/\/ armazena o resultado no cache\n\n    return result;\n  };\n}\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4f7f\u7528\u793a\u4f8b\uff1a<\/p>\n<pre>function factorial(n) {\n  if (n &lt;= 1) return 1;\n  return n * factorial(n - 1);\n}\n\nconst memoizedFactorial = memoize(factorial);\n\nconsole.log(memoizedFactorial(5)); \/\/ Calcula e armazena em cache\nconsole.log(memoizedFactorial(5)); \/\/ Recupera do cache\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u8bb0\u5fc6\u7684\u597d\u5904<\/strong><\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<ul>\n<li>\u51cf\u5c11\u5904\u7406\uff1a\u907f\u514d\u91cd\u590d\u8ba1\u7b97\uff0c\u4ece\u800c\u63d0\u9ad8\u6027\u80fd\u3002<\/li>\n<li>\u9012\u5f52\u51fd\u6570\u7684\u7406\u60f3\u9009\u62e9\uff1a\u52a0\u5feb\u9700\u8981\u4f7f\u7528\u76f8\u540c\u503c\u591a\u6b21\u8c03\u7528\u7684\u51fd\u6570\u7684\u5904\u7406\u901f\u5ea6\u3002<\/li>\n<li>\u7eaf\u51fd\u6570\u7684\u9ad8\u6548\uff1a\u5f53\u51fd\u6570\u4e3a\u76f8\u540c\u7684\u53c2\u6570\u8fd4\u56de\u76f8\u540c\u7684\u503c\u65f6\uff0c\u8bb0\u5fc6\u6548\u679c\u6700\u597d\u3002 \u8bb0\u5fc6\u7684\u5e38\u89c1\u5e94\u7528<\/li>\n<li>\u6570\u5b66\u8ba1\u7b97\uff1a\u5982\u9636\u4e58\u3001\u6590\u6ce2\u90a3\u5951\u7b49<\/li>\n<li>api \u8c03\u7528\uff1a\u9632\u6b62\u4f7f\u7528\u76f8\u540c\u53c2\u6570\u91cd\u590d\u8c03\u7528 api\u3002<\/li>\n<li>\u9ad8\u6210\u672c\u8fd0\u8425\uff1a\u6570\u636e\u5904\u7406\u548c\u590d\u6742\u64cd\u4f5c\u3002<\/li>\n<\/ul>\n<p><strong>\u603b\u7ed3<\/strong><br \/> javascript \u4e2d\u7684\u8bb0\u5fc6\u662f\u4e00\u79cd\u5f3a\u5927\u7684\u6280\u672f\uff0c\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u6548\u7387\u5e76\u52a0\u901f\u91cd\u590d\u6027\u3001\u8ba1\u7b97\u91cf\u5927\u7684\u64cd\u4f5c\u7684\u6027\u80fd\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fJavaScript \u4e2d\u7684\u8bb0\u5fc6\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>\u8bb0\u5fc6\u5316\uff08\u6216\u201c\u8bb0\u5fc6\u5316\u201d\uff09\u662f\u4e00\u79cd\u4f18\u5316\u6280\u672f\uff0c\u53ef\u4ee5\u7f13\u5b58\u7e41\u91cd\u6216\u8017\u65f6\u7684\u51fd\u6570\u7684\u7ed3\u679c\uff0c\u4ee5\u4fbf\u5c06\u6765\u4f7f\u7528\u76f8\u540c\u53c2\u6570\u7684\u8c03\u7528\u66f4\u5feb\u3002\u8bb0\u5fc6\u5728\u7eaf\u51fd\u6570\u4e2d\u7279\u522b\u6709\u7528\uff0c\u56e0\u4e3a\u540c\u4e00\u7ec4\u8f93\u5165\u603b\u662f\u4ea7\u751f\u76f8\u540c\u7684\u7ed3\u679c\u3002 \u8bb0\u5fc6\u662f\u5982\u4f55\u8fd0\u4f5c\u7684\uff1f \u5f53\u7b2c\u4e00\u6b21\u4f7f\u7528\u67d0\u4e9b\u53c2\u6570\u8c03\u7528\u8bb0\u5fc6\u7684\u51fd\u6570\u65f6\uff0c\u5b83\u4f1a\u6267\u884c\u8ba1\u7b97\u5e76\u5c06\u7ed3\u679c\u5b58\u50a8\u5728\u7f13\u5b58\u4e2d\u3002\u540e\u7eed\u8c03\u7528\u76f8\u540c\u53c2\u6570\u65f6\uff0c\u51fd\u6570\u4f1a\u76f4\u63a5\u4ece\u7f13\u5b58\u4e2d\u8fd4\u56de\u7ed3\u679c\uff0c\u907f\u514d\u91cd\u590d\u8ba1\u7b97\u3002 \u57fa\u672c\u8bb0\u5fc6\u5b9e\u73b0 function memoize(fn) { const cache = {}; return function(&#8230;args) { const key = json.stringify(args); \/\/ cria uma chave \u00fanica para os argumentos if (cache[key]) { console.log(&#8220;resultado em cache&#8221;); return cache[key]; \/\/ retorna o valor armazenado no cache } const result = fn(&#8230;args); \/\/ calcula o valor cache[key] = result; \/\/ armazena [&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-2635","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/2635","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=2635"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/2635\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=2635"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=2635"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=2635"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}