2016-06-22 27 views
1

documentation here says that是什麼在IDBCursor IndexedDB的

關鍵

的關鍵和PrimaryKey的之間的區別返回遊標的當前關鍵。 (遊標也有鑰匙和代表的關鍵和最後一個迭代記錄的值的值。)

的PrimaryKey

返回遊標的現行有效的關鍵。 (如果光標的來源是對象存儲,則光標的有效對象存儲是對象存儲,並且光標的有效鍵是光標的位置。)

但在下面的示例中,正是使用同樣的,我也得到相同的值都:

那麼究竟是什麼實際的區別?

回答

3

如果您正在遍歷對象存儲,它們是相同的。

如果你迭代的指數,key索引鍵primaryKey對象存儲的關鍵。

例如:

book_store = db.createObjectStore('books'); 
title_index = store.createIndex('by_title', 'title'); 

key = 123; 
value = {title: 'IDB for Dummies', author: 'Alice'}; 
book_store.put(value, key); 

book_store.openCursor().onsuccess = function(e) { 
    cursor = e.target.result; 
    console.log(cursor.key); // logs 123 
    console.log(cursor.primaryKey); // logs 123 
}; 
title_index.openCursor().onsuccess = function(e) { 
    cursor = e.target.result; 
    console.log(cursor.key); // logs 'IDB for Newbies' 
    console.log(cursor.primaryKey); // logs 123 
};