IndexedDB 解釋道。
在上一篇文章中,我們討論了 Dexie,IndexedDB 的包裝器。在本文中,我們討論 IndexedDB。您必須熟悉這個 localStorage API,通常用於在瀏覽器中儲存資訊。類似地,IndexedDB 用於客戶端儲存。
什麼是 IndexedDB?
MDN文件說明:
IndexedDB 是一個低階 API,用於客戶端儲存大量結構化資料(包括檔案/blob)。此 API 使用索引來實現對此資料的高效能搜尋。雖然 Web 儲存對於儲存少量資料很有用,但對於儲存大量結構化資料則不太有用。
IndexedDB 提供了一個解決方案。這是 MDN IndexedDB 報告的主要登陸頁面 - 在這裡我們提供完整 API 參考和使用指南、瀏覽器支援詳細資訊以及關鍵概念的一些解釋的連結。
範例儲存庫:
MDN 提供了一個範例 Github 儲存庫,並有 script/todo.js。
使用 window.onload 初始化腳本
window.onload = () => { }
開啟資料庫要求:
// Let us open our database const DBOpenRequest = window.indexedDB.open('toDoList', 4);
連線錯誤:
// Register two event handlers to act on the database being opened successfully, or not DBOpenRequest.onerror = (event) => { note.appendChild(createListItem('Error loading database.')); };
資料庫連線成功時:
DBOpenRequest.onsuccess = (event) => { note.appendChild(createListItem('Database initialised.')); // Store the result of opening the database in the db variable. This is used a lot below db = DBOpenRequest.result; // Run the displayData() function to populate the task list with all the to-do list data already in the IndexedDB displayData(); };
新增資料
// Open a read/write DB transaction, ready for adding the data const transaction = db.transaction(['toDoList'], 'readwrite'); // Call an object store that's already been added to the database const objectStore = transaction.objectStore('toDoList'); // Make a request to add our newItem object to the object store const objectStoreRequest = objectStore.add(newItem[0]); objectStoreRequest.onsuccess = (event) => { // process data on success. } // Report on the success of the transaction completing, when everything is done transaction.oncomplete = () => { note.appendChild(createListItem('Transaction completed: database modification finished.')); // Update the display of data to show the newly added item, by running displayData() again. displayData(); }; // Handler for any unexpected error transaction.onerror = () => { note.appendChild(createListItem(`Transaction not opened due to error: ${transaction.error}`)); };
觀察:localStorage 與 IndexedDB
您現在可能已經意識到,僅添加一條記錄就需要大量程式碼,您有非同步回調,例如 onerror 和 onsuccess。這個堆疊交換答案中指出了這一點。
為了簡化處理此 IndexedDB,可以使用 Dexie。
使用 Dexie 新增資料:
export function AddFriendForm({ defaultAge } = { defaultAge: 21 }) { const [name, setName] = useState(''); const [age, setAge] = useState(defaultAge); const [status, setStatus] = useState(''); async function addFriend() { try { // Add the new friend! const id = await db.friends.add({ name, age }); setStatus(`Friend ${name} successfully added. Got id ${id}`); setName(''); setAge(defaultAge); } catch (error) { setStatus(`Failed to add ${name}: ${error}`); } } return ( <> <p>{status}</p> Name: <input type="text" value={name} onChange={(ev) => setName(ev.target.value)} /> Age: <input type="number" value={age} onChange={(ev) => setAge(Number(ev.target.value))} /> <button onClick={addFriend}>Add</button> </> ); }
這個包裝 API 讓我想起了 Prisma 和 Drizzle 等 ORM。
關於我們:
在 Thinkthroo,我們研究大型開源專案並提供架構指南。我們開發了使用 tailwind 建立的 resubale 元件,您可以在您的專案中使用它們。我們提供 Next.js、React 和 Node 開發服務。
與我們預約會面討論您的專案。
參考資料:
https://www.reddit.com/r/sveltejs/comments/15rj12h/any_downsides_to_using_indexeddb_vs_localstorage/
https://developer.mozilla.org/en-US/docs/Web/API/IndexedDB_API
https://github.com/mdn/dom-examples/tree/main/to-do-notifications
https://softwareengineering.stackexchange.com/questions/219953/how-is-localstorage-與indexeddb不同
以上是IndexedDB 解釋道。的詳細內容。更多資訊請關注PHP中文網其他相關文章!

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

JavaScript是現代Web開發的基石,它的主要功能包括事件驅動編程、動態內容生成和異步編程。 1)事件驅動編程允許網頁根據用戶操作動態變化。 2)動態內容生成使得頁面內容可以根據條件調整。 3)異步編程確保用戶界面不被阻塞。 JavaScript廣泛應用於網頁交互、單頁面應用和服務器端開發,極大地提升了用戶體驗和跨平台開發的靈活性。

JavaScript的最新趨勢包括TypeScript的崛起、現代框架和庫的流行以及WebAssembly的應用。未來前景涵蓋更強大的類型系統、服務器端JavaScript的發展、人工智能和機器學習的擴展以及物聯網和邊緣計算的潛力。

不同JavaScript引擎在解析和執行JavaScript代碼時,效果會有所不同,因為每個引擎的實現原理和優化策略各有差異。 1.詞法分析:將源碼轉換為詞法單元。 2.語法分析:生成抽象語法樹。 3.優化和編譯:通過JIT編譯器生成機器碼。 4.執行:運行機器碼。 V8引擎通過即時編譯和隱藏類優化,SpiderMonkey使用類型推斷系統,導致在相同代碼上的性能表現不同。

JavaScript是現代Web開發的核心語言,因其多樣性和靈活性而廣泛應用。 1)前端開發:通過DOM操作和現代框架(如React、Vue.js、Angular)構建動態網頁和單頁面應用。 2)服務器端開發:Node.js利用非阻塞I/O模型處理高並發和實時應用。 3)移動和桌面應用開發:通過ReactNative和Electron實現跨平台開發,提高開發效率。

Python更適合初學者,學習曲線平緩,語法簡潔;JavaScript適合前端開發,學習曲線較陡,語法靈活。 1.Python語法直觀,適用於數據科學和後端開發。 2.JavaScript靈活,廣泛用於前端和服務器端編程。

本文展示了與許可證確保的後端的前端集成,並使用Next.js構建功能性Edtech SaaS應用程序。 前端獲取用戶權限以控制UI的可見性並確保API要求遵守角色庫

從C/C 轉向JavaScript需要適應動態類型、垃圾回收和異步編程等特點。 1)C/C 是靜態類型語言,需手動管理內存,而JavaScript是動態類型,垃圾回收自動處理。 2)C/C 需編譯成機器碼,JavaScript則為解釋型語言。 3)JavaScript引入閉包、原型鍊和Promise等概念,增強了靈活性和異步編程能力。

JavaScript不需要安裝,因為它已內置於現代瀏覽器中。你只需文本編輯器和瀏覽器即可開始使用。 1)在瀏覽器環境中,通過標籤嵌入HTML文件中運行。 2)在Node.js環境中,下載並安裝Node.js後,通過命令行運行JavaScript文件。
