Skip to content

Commit 6f17004

Browse files
Replace var with let/const in examples
Slightly modernize the example code, and fixes an issue noted in: #209 (comment)
1 parent d3943e1 commit 6f17004

File tree

1 file changed

+49
-47
lines changed

1 file changed

+49
-47
lines changed

index.bs

Lines changed: 49 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -142,14 +142,15 @@ object store and indexes. Finally, the opened connection is saved for
142142
use in subsequent examples.
143143

144144
```js
145-
var request = indexedDB.open("library");
145+
const request = indexedDB.open("library");
146+
let db;
146147

147148
request.onupgradeneeded = function() {
148149
// The database did not previously exist, so create object stores and indexes.
149-
var db = request.result;
150-
var store = db.createObjectStore("books", {keyPath: "isbn"});
151-
var titleIndex = store.createIndex("by_title", "title", {unique: true});
152-
var authorIndex = store.createIndex("by_author", "author");
150+
const db = request.result;
151+
const store = db.createObjectStore("books", {keyPath: "isbn"});
152+
const titleIndex = store.createIndex("by_title", "title", {unique: true});
153+
const authorIndex = store.createIndex("by_author", "author");
153154

154155
// Populate with initial data.
155156
store.put({title: "Quarry Memories", author: "Fred", isbn: 123456});
@@ -165,8 +166,8 @@ request.onsuccess = function() {
165166
The following example populates the database using a transaction.
166167

167168
```js
168-
var tx = db.transaction("books", "readwrite");
169-
var store = tx.objectStore("books");
169+
const tx = db.transaction("books", "readwrite");
170+
const store = tx.objectStore("books");
170171

171172
store.put({title: "Quarry Memories", author: "Fred", isbn: 123456});
172173
store.put({title: "Water Buffaloes", author: "Fred", isbn: 234567});
@@ -181,13 +182,13 @@ The following example looks up a single book in the database by title
181182
using an index.
182183

183184
```js
184-
var tx = db.transaction("books", "readonly");
185-
var store = tx.objectStore("books");
186-
var index = store.index("by_title");
185+
const tx = db.transaction("books", "readonly");
186+
const store = tx.objectStore("books");
187+
const index = store.index("by_title");
187188

188-
var request = index.get("Bedrock Nights");
189+
const request = index.get("Bedrock Nights");
189190
request.onsuccess = function() {
190-
var matching = request.result;
191+
const matching = request.result;
191192
if (matching !== undefined) {
192193
// A match was found.
193194
report(matching.isbn, matching.title, matching.author);
@@ -202,13 +203,13 @@ The following example looks up all books in the database by author
202203
using an index and a cursor.
203204

204205
```js
205-
var tx = db.transaction("books", "readonly");
206-
var store = tx.objectStore("books");
207-
var index = store.index("by_author");
206+
const tx = db.transaction("books", "readonly");
207+
const store = tx.objectStore("books");
208+
const index = store.index("by_author");
208209

209-
var request = index.openCursor(IDBKeyRange.only("Fred"));
210+
const request = index.openCursor(IDBKeyRange.only("Fred"));
210211
request.onsuccess = function() {
211-
var cursor = request.result;
212+
const cursor = request.result;
212213
if (cursor) {
213214
// Called for each matching record.
214215
report(cursor.value.isbn, cursor.value.title, cursor.value.author);
@@ -224,9 +225,9 @@ The following example shows how errors could be handled when a request
224225
fails.
225226

226227
```js
227-
var tx = db.transaction("books", "readwrite");
228-
var store = tx.objectStore("books");
229-
var request = store.put({title: "Water Buffaloes", author: "Slate", isbn: 987654});
228+
const tx = db.transaction("books", "readwrite");
229+
const store = tx.objectStore("books");
230+
const request = store.put({title: "Water Buffaloes", author: "Slate", isbn: 987654});
230231
request.onerror = function(event) {
231232
// The uniqueness constraint of the "by_title" index failed.
232233
report(request.error);
@@ -249,26 +250,27 @@ stores and indexes. The following example shows one way to handle
249250
migrating from an older version of the database.
250251

251252
```js
252-
var request = indexedDB.open("library", 3); // Request version 3.
253+
const request = indexedDB.open("library", 3); // Request version 3.
254+
let db;
253255

254256
request.onupgradeneeded = function(event) {
255-
var db = request.result;
257+
const db = request.result;
256258
if (event.oldVersion < 1) {
257259
// Version 1 is the first version of the database.
258-
var store = db.createObjectStore("books", {keyPath: "isbn"});
259-
var titleIndex = store.createIndex("by_title", "title", {unique: true});
260-
var authorIndex = store.createIndex("by_author", "author");
260+
const store = db.createObjectStore("books", {keyPath: "isbn"});
261+
const titleIndex = store.createIndex("by_title", "title", {unique: true});
262+
const authorIndex = store.createIndex("by_author", "author");
261263
}
262264
if (event.oldVersion < 2) {
263265
// Version 2 introduces a new index of books by year.
264-
var bookStore = request.transaction.objectStore("books");
265-
var yearIndex = bookStore.createIndex("by_year", "year");
266+
const bookStore = request.transaction.objectStore("books");
267+
const yearIndex = bookStore.createIndex("by_year", "year");
266268
}
267269
if (event.oldVersion < 3) {
268270
// Version 3 introduces a new object store for magazines with two indexes.
269-
var magazines = db.createObjectStore("magazines");
270-
var publisherIndex = magazines.createIndex("by_publisher", "publisher");
271-
var frequencyIndex = magazines.createIndex("by_frequency", "frequency");
271+
const magazines = db.createObjectStore("magazines");
272+
const publisherIndex = magazines.createIndex("by_publisher", "publisher");
273+
const frequencyIndex = magazines.createIndex("by_frequency", "frequency");
272274
}
273275
};
274276

@@ -342,8 +344,8 @@ event fires if other clients still hold a connection to the database after their
342344
<a event>`versionchange`</a> events have fired.
343345

344346
```js
345-
var request = indexedDB.open("library", 4); // Request version 4.
346-
var blockedTimeout;
347+
const request = indexedDB.open("library", 4); // Request version 4.
348+
let blockedTimeout;
347349

348350
request.onblocked = function() {
349351
// Give the other clients time to save data asynchronously.
@@ -1754,10 +1756,10 @@ property is assigned a value of 1 because that is the next [=/key=]
17541756
generated by the [=key generator=].
17551757

17561758
```js
1757-
var store = db.createObjectStore("store", { keyPath: "foo.bar",
1758-
autoIncrement: true });
1759+
const store = db.createObjectStore("store", { keyPath: "foo.bar",
1760+
autoIncrement: true });
17591761
store.put({ foo: {} }).onsuccess = function(e) {
1760-
var key = e.target.result;
1762+
const key = e.target.result;
17611763
console.assert(key === 1);
17621764
};
17631765
```
@@ -1779,10 +1781,10 @@ store=] is "`foo.bar`". The actual object has a value of
17791781
key value.
17801782

17811783
```js
1782-
var store = db.createObjectStore("store", { keyPath: "foo.bar",
1783-
autoIncrement: true });
1784+
const store = db.createObjectStore("store", { keyPath: "foo.bar",
1785+
autoIncrement: true });
17841786
store.put({ foo: { bar: 10 } }).onsuccess = function(e) {
1785-
var key = e.target.result;
1787+
const key = e.target.result;
17861788
console.assert(key === 10);
17871789
};
17881790
```
@@ -1804,13 +1806,13 @@ properties are created each as a child of the other until a value for
18041806
store.
18051807

18061808
```js
1807-
var store = db.createObjectStore("store", { keyPath: "foo.bar.baz",
1808-
autoIncrement: true });
1809+
const store = db.createObjectStore("store", { keyPath: "foo.bar.baz",
1810+
autoIncrement: true });
18091811
store.put({ zip: {} }).onsuccess = function(e) {
1810-
var key = e.target.result;
1812+
const key = e.target.result;
18111813
console.assert(key === 1);
18121814
store.get(key).onsuccess = function(e) {
1813-
var value = e.target.result;
1815+
const value = e.target.result;
18141816
// value will be: { zip: {}, foo: { bar: { baz: 1 } } }
18151817
console.assert(value.foo.bar.baz === 1);
18161818
};
@@ -1827,7 +1829,7 @@ the actual object is an array, `[10]`. Trying to define a
18271829
property on the array fails.
18281830

18291831
```js
1830-
var store = db.createObjectStore("store", { keyPath: "foo", autoIncrement: true });
1832+
const store = db.createObjectStore("store", { keyPath: "foo", autoIncrement: true });
18311833

18321834
// The key generation will attempt to create and store the key path
18331835
// property on this primitive.
@@ -1980,7 +1982,7 @@ Various event handlers are registered for responding to various
19801982
situations.
19811983

19821984
```js
1983-
var request = indexedDB.open('AddressBook', 15);
1985+
const request = indexedDB.open('AddressBook', 15);
19841986
request.onsuccess = function(evt) {...};
19851987
request.onerror = function(evt) {...};
19861988
```
@@ -3595,9 +3597,9 @@ must be used as error.
35953597
The asynchronous creation of indexes is observable in the following example:
35963598

35973599
```js
3598-
var request1 = objectStore.put({name: "betty"}, 1);
3599-
var request2 = objectStore.put({name: "betty"}, 2);
3600-
var index = objectStore.createIndex("by_name", "name", {unique: true});
3600+
const request1 = objectStore.put({name: "betty"}, 1);
3601+
const request2 = objectStore.put({name: "betty"}, 2);
3602+
const index = objectStore.createIndex("by_name", "name", {unique: true});
36013603
```
36023604

36033605
At the point where {{createIndex()}} called, neither of the

0 commit comments

Comments
 (0)