IDBObjectStore.name

admin發表於2019-09-18

此屬性可以返回當前物件倉庫的name屬性值,也就是名稱。

通過createObjectStore()方法建立物件倉庫時,第一個引數規定名稱。

具體可以參閱createObjectStore() 建立物件倉庫一章節。

語法結構:

[JavaScript] 純文字檢視 複製程式碼
let objectStoreName = IDBObjectStore.name;

用法比較簡單,看如下程式碼例項:

[HTML] 純文字檢視 複製程式碼執行程式碼
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="author" content="http://www.softwhy.com/" />
<title>螞蟻部落</title>
<script>
let request = window.indexedDB.open("antzone", 1);
request.onupgradeneeded = (ev) => {
  let db = ev.target.result;
  if (!db.objectStoreNames.contains('students')) {
    let objectStore = db.createObjectStore('students',{keyPath:"id"});
    objectStore.createIndex("xingbie","sex",{ unique: false });
  }
}

request.onsuccess = (ev) => {
  let db = ev.target.result;
  let transaction = db.transaction(['students'], 'readonly');
  let objectStore = transaction.objectStore('students');
  console.log(objectStore.name);
}
</script>
</head>
<body>
  <p>列印物件倉庫的名稱</p>
</body>
</html>

程式碼執行效果截圖如下:

a:3:{s:3:\"pic\";s:43:\"portal/201909/18/174641u1v69qqvkm6rwvf4.jpg\";s:5:\"thumb\";s:0:\"\";s:6:\"remote\";N;}

上述程式碼可以列印出物件倉庫的名稱。