{"id":3138,"date":"2024-11-10T17:34:32","date_gmt":"2024-11-10T09:34:32","guid":{"rendered":"https:\/\/fwq.ai\/blog\/3138\/"},"modified":"2024-11-10T17:34:32","modified_gmt":"2024-11-10T09:34:32","slug":"js%e5%a6%82%e4%bd%95%e7%bc%93%e5%ad%98","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/3138\/","title":{"rendered":"js\u5982\u4f55\u7f13\u5b58"},"content":{"rendered":"<blockquote><p>\n  javascript \u7f13\u5b58\u6280\u672f\u5305\u62ec\uff1a\u672c\u5730\u5b58\u50a8\uff1a\u7528\u4e8e\u6301\u4e45\u5b58\u50a8 key-value \u5bf9\u3002\u4f1a\u8bdd\u5b58\u50a8\uff1a\u4e0e\u672c\u5730\u5b58\u50a8\u7c7b\u4f3c\uff0c\u4f46\u4ec5\u5728\u4f1a\u8bdd\u671f\u95f4\u4fdd\u7559\u6570\u636e\u3002indexeddb\uff1a\u7528\u4e8e\u5b58\u50a8\u5927\u91cf\u7ed3\u6784\u5316\u6570\u636e\uff0c\u66f4\u590d\u6742\u4f46\u529f\u80fd\u66f4\u5f3a\u5927\u3002service worker\uff1a\u7528\u4e8e\u7f13\u5b58\u54cd\u5e94\uff0c\u5b9e\u73b0\u79bb\u7ebf\u4f53\u9a8c\u548c\u63d0\u9ad8\u6027\u80fd\u3002\u7b2c\u4e09\u65b9\u5e93\uff1a\u63d0\u4f9b\u9644\u52a0\u529f\u80fd\u548c\u4fbf\u5229\u6027\uff0c\u5982 cache-control\u3001pouchdb \u548c lokijs\u3002\u6839\u636e\u5e94\u7528\u7a0b\u5e8f\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u6280\u672f\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/08\/2024110804453168671.jpg\" class=\"aligncenter\" title=\"js\u5982\u4f55\u7f13\u5b58\u63d2\u56fe\" alt=\"js\u5982\u4f55\u7f13\u5b58\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u4f7f\u7528 JavaScript \u8fdb\u884c\u7f13\u5b58<\/strong><\/p>\n<p><strong>\u7b80\u4ecb<\/strong><br \/>\u7f13\u5b58\u662f\u4e00\u79cd\u4f18\u5316\u6280\u672f\uff0c\u7528\u4e8e\u5b58\u50a8\u7ecf\u5e38\u8bbf\u95ee\u7684\u6570\u636e\u4ee5\u63d0\u9ad8\u5e94\u7528\u7a0b\u5e8f\u6027\u80fd\u3002\u5728 JavaScript \u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u6280\u672f\u6765\u5b9e\u73b0\u7f13\u5b58\u3002<\/p>\n<p><strong>\u672c\u5730\u5b58\u50a8<\/strong><br \/>\u672c\u5730\u5b58\u50a8\u662f\u4e00\u79cd\u5728\u6d4f\u89c8\u5668\u4e2d\u5b58\u50a8\u6570\u636e\u7684\u7b80\u5355\u65b9\u6cd5\u3002\u5b83\u4f7f\u7528 key-value \u5bf9\uff0c\u652f\u6301\u5b57\u7b26\u4e32\u3001\u6570\u5b57\u548c\u5e03\u5c14\u503c\u7b49\u6570\u636e\u7c7b\u578b\u3002<\/p>\n<pre>\/\/ \u8bbe\u7f6e\u7f13\u5b58\nlocalStorage.setItem(\"name\", \"John Doe\");\n\n\/\/ \u83b7\u53d6\u7f13\u5b58\nconst name = localStorage.getItem(\"name\");<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u4f1a\u8bdd\u5b58\u50a8<\/strong><br \/>\u4f1a\u8bdd\u5b58\u50a8\u4e0e\u672c\u5730\u5b58\u50a8\u7c7b\u4f3c\uff0c\u4f46\u5b83\u5728\u6d4f\u89c8\u5668\u9009\u9879\u5361\u6216\u7a97\u53e3\u5173\u95ed\u65f6\u4f1a\u88ab\u6e05\u9664\u3002\u5b83\u9002\u5408\u5b58\u50a8\u4e34\u65f6\u6570\u636e\uff0c\u4f8b\u5982\u7528\u6237\u4f1a\u8bdd\u4fe1\u606f\u3002<\/p>\n<pre>\/\/ \u8bbe\u7f6e\u7f13\u5b58\nsessionStorage.setItem(\"token\", \"abc123\");\n\n\/\/ \u83b7\u53d6\u7f13\u5b58\nconst token = sessionStorage.getItem(\"token\");<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>IndexedDB<\/strong><br \/>IndexedDB \u662f\u4e00\u4e2a\u9ad8\u7ea7 API\uff0c\u7528\u4e8e\u5728\u6d4f\u89c8\u5668\u4e2d\u5b58\u50a8\u5927\u91cf\u7ed3\u6784\u5316\u6570\u636e\u3002\u5b83\u6bd4\u672c\u5730\u5b58\u50a8\u6216\u4f1a\u8bdd\u5b58\u50a8\u66f4\u5f3a\u5927\uff0c\u4f46\u4f7f\u7528\u8d77\u6765\u4e5f\u66f4\u590d\u6742\u3002<\/p>\n<pre>\/\/ \u6253\u5f00 IndexedDB \u6570\u636e\u5e93\nconst request = indexedDB.open(\"myDB\", 1);\n\nrequest.onsuccess = (e) =&gt; {\n  \/\/ \u64cd\u4f5c\u6570\u636e\u5e93\n};<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>Service Worker<\/strong><br \/>Service Worker \u662f JavaScript worker\uff0c\u5728\u540e\u53f0\u8fd0\u884c\u5e76\u62e6\u622a\u7f51\u7edc\u8bf7\u6c42\u3002\u5b83\u4eec\u53ef\u4ee5\u7528\u4e8e\u7f13\u5b58\u54cd\u5e94\u4ee5\u5b9e\u73b0\u79bb\u7ebf\u4f53\u9a8c\u548c\u63d0\u9ad8\u6027\u80fd\u3002<\/p>\n<pre>\/\/ Service Worker \u6ce8\u518c\nif (\"serviceWorker\" in navigator) {\n  navigator.serviceWorker.register(\"my-worker.js\");\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u7b2c\u4e09\u65b9\u5e93<\/strong><br \/>\u8fd8\u6709\u4e00\u4e9b\u7b2c\u4e09\u65b9\u5e93\u53ef\u4ee5\u7b80\u5316 JavaScript \u7f13\u5b58\uff0c\u4f8b\u5982\uff1a<\/p>\n<ul>\n<li> <strong>Cache-Control:<\/strong> https:\/\/hub.com\/1inaction\/cache-control-js<\/li>\n<li> <strong>PouchDB:<\/strong> https:\/\/pouchdb.com\/<\/li>\n<li> <strong>LokiJS:<\/strong> https:\/\/lokijs.org\/<\/li>\n<\/ul>\n<p><strong>\u9009\u62e9\u5408\u9002\u7684\u7f13\u5b58\u6280\u672f<\/strong><br \/>\u9009\u62e9\u5408\u9002\u7684\u7f13\u5b58\u6280\u672f\u53d6\u51b3\u4e8e\u5e94\u7528\u7a0b\u5e8f\u7684\u9700\u6c42\u3002\u672c\u5730\u5b58\u50a8\u548c\u4f1a\u8bdd\u5b58\u50a8\u7b80\u5355\u6613\u7528\uff0c\u4f46\u5bb9\u91cf\u6709\u9650\u3002IndexedDB \u66f4\u5f3a\u5927\uff0c\u4f46\u66f4\u4e3a\u590d\u6742\u3002Service Worker \u9002\u7528\u4e8e\u79bb\u7ebf\u7f13\u5b58\u3002\u7b2c\u4e09\u65b9\u5e93\u63d0\u4f9b\u4e86\u989d\u5916\u7684\u529f\u80fd\u548c\u4fbf\u5229\u6027\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fjs\u5982\u4f55\u7f13\u5b58\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>javascript \u7f13\u5b58\u6280\u672f\u5305\u62ec\uff1a\u672c\u5730\u5b58\u50a8\uff1a\u7528\u4e8e\u6301\u4e45\u5b58\u50a8 key-value \u5bf9\u3002\u4f1a\u8bdd\u5b58\u50a8\uff1a\u4e0e\u672c\u5730\u5b58\u50a8\u7c7b\u4f3c\uff0c\u4f46\u4ec5\u5728\u4f1a\u8bdd\u671f\u95f4\u4fdd\u7559\u6570\u636e\u3002indexeddb\uff1a\u7528\u4e8e\u5b58\u50a8\u5927\u91cf\u7ed3\u6784\u5316\u6570\u636e\uff0c\u66f4\u590d\u6742\u4f46\u529f\u80fd\u66f4\u5f3a\u5927\u3002service worker\uff1a\u7528\u4e8e\u7f13\u5b58\u54cd\u5e94\uff0c\u5b9e\u73b0\u79bb\u7ebf\u4f53\u9a8c\u548c\u63d0\u9ad8\u6027\u80fd\u3002\u7b2c\u4e09\u65b9\u5e93\uff1a\u63d0\u4f9b\u9644\u52a0\u529f\u80fd\u548c\u4fbf\u5229\u6027\uff0c\u5982 cache-control\u3001pouchdb \u548c lokijs\u3002\u6839\u636e\u5e94\u7528\u7a0b\u5e8f\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u6280\u672f\u3002 \u5982\u4f55\u4f7f\u7528 JavaScript \u8fdb\u884c\u7f13\u5b58 \u7b80\u4ecb\u7f13\u5b58\u662f\u4e00\u79cd\u4f18\u5316\u6280\u672f\uff0c\u7528\u4e8e\u5b58\u50a8\u7ecf\u5e38\u8bbf\u95ee\u7684\u6570\u636e\u4ee5\u63d0\u9ad8\u5e94\u7528\u7a0b\u5e8f\u6027\u80fd\u3002\u5728 JavaScript \u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u6280\u672f\u6765\u5b9e\u73b0\u7f13\u5b58\u3002 \u672c\u5730\u5b58\u50a8\u672c\u5730\u5b58\u50a8\u662f\u4e00\u79cd\u5728\u6d4f\u89c8\u5668\u4e2d\u5b58\u50a8\u6570\u636e\u7684\u7b80\u5355\u65b9\u6cd5\u3002\u5b83\u4f7f\u7528 key-value \u5bf9\uff0c\u652f\u6301\u5b57\u7b26\u4e32\u3001\u6570\u5b57\u548c\u5e03\u5c14\u503c\u7b49\u6570\u636e\u7c7b\u578b\u3002 \/\/ \u8bbe\u7f6e\u7f13\u5b58 localStorage.setItem(&#8220;name&#8221;, &#8220;John Doe&#8221;); \/\/ \u83b7\u53d6\u7f13\u5b58 const name = localStorage.getItem(&#8220;name&#8221;); \u767b\u5f55\u540e\u590d\u5236 \u4f1a\u8bdd\u5b58\u50a8\u4f1a\u8bdd\u5b58\u50a8\u4e0e\u672c\u5730\u5b58\u50a8\u7c7b\u4f3c\uff0c\u4f46\u5b83\u5728\u6d4f\u89c8\u5668\u9009\u9879\u5361\u6216\u7a97\u53e3\u5173\u95ed\u65f6\u4f1a\u88ab\u6e05\u9664\u3002\u5b83\u9002\u5408\u5b58\u50a8\u4e34\u65f6\u6570\u636e\uff0c\u4f8b\u5982\u7528\u6237\u4f1a\u8bdd\u4fe1\u606f\u3002 \/\/ \u8bbe\u7f6e\u7f13\u5b58 sessionStorage.setItem(&#8220;token&#8221;, &#8220;abc123&#8221;); \/\/ \u83b7\u53d6\u7f13\u5b58 const token = sessionStorage.getItem(&#8220;token&#8221;); \u767b\u5f55\u540e\u590d\u5236 IndexedDBIndexedDB \u662f\u4e00\u4e2a\u9ad8\u7ea7 API\uff0c\u7528\u4e8e\u5728\u6d4f\u89c8\u5668\u4e2d\u5b58\u50a8\u5927\u91cf\u7ed3\u6784\u5316\u6570\u636e\u3002\u5b83\u6bd4\u672c\u5730\u5b58\u50a8\u6216\u4f1a\u8bdd\u5b58\u50a8\u66f4\u5f3a\u5927\uff0c\u4f46\u4f7f\u7528\u8d77\u6765\u4e5f\u66f4\u590d\u6742\u3002 \/\/ \u6253\u5f00 IndexedDB \u6570\u636e\u5e93 const request = indexedDB.open(&#8220;myDB&#8221;, 1); request.onsuccess = [&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-3138","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/3138","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=3138"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/3138\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=3138"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=3138"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=3138"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}