hoon.WebStorage Class
The Base Class of hoon.localStorage and hoon.sessionStorage. You cannot access this class.
This class extends the Storage Interface. In the Storage interface, you must typically use the JSON object for parsing and serializing. On the other hand, in this class you don't have to use the JSON object. This class internally parses and serializes data using the hoon.json object.
Additionally, this class provides get, set and remove methods for operating multiple keys.
Constructor
hoon.WebStorage
-
webstorage
Parameters:
-
webstorage
LocalStorage | SessionStorage
Methods
clear
()
This method is equivalent to webstorage.clear()
.
Example:
hoon.localStorage.setItem("name", "John");
hoon.localStorage.clear();
get
-
keys
Warning: The following methods must be used together.
- WebStorage.set()
- WebStorage.get()
- WebStorage.setItem()
- WebStorage.getItem()
For example, the following code doesn't work as expected.
localStorage.setItem("name", "John"); var name = hoon.localStorage.getItem("name", "John");
Otherwise, the following code works as expected.
hoon.localStorage.setItem("name", "John"); var name = hoon.localStorage.getItem("name", "John");
Parameters:
-
keys
Null | Array of String | String
Example:
hoon.localStorage.set({ name: "John", id: -1 });
// equivalent to hoon.localStorage.getItem("name");
hoon.localStorage.get("name"); // "John"
hoon.localStorage.get(["name", "id"]); // { name: "John", id: -1 }
hoon.localStorage.get(); // { name: "John", id: -1 }
// equivalent to hoon.localStorage.get();
hoon.localStorage.get(null); // { name: "John", id: -1 }
getItem
-
key
return the current value associated with the given key. If the given key does not exist in the list associated with the object then this method must return null.
Warning: The following methods must be used together.
- WebStorage.set()
- WebStorage.get()
- WebStorage.setItem()
- WebStorage.getItem()
For example, the following code doesn't work as expected.
localStorage.setItem("name", "John"); var name = hoon.localStorage.getItem("name", "John");
Otherwise, the following code works as expected.
hoon.localStorage.setItem("name", "John"); var name = hoon.localStorage.getItem("name", "John");
Parameters:
-
key
String
Returns:
the current value associated with the given key.
Example:
hoon.localStorage.setItem("obj", {
name: "John",
number: 1,
});
var obj = hoon.localStorage.getItem("obj");
console.log();
key
-
n
This method is equivalent to webstorage.key(n)
.
Parameters:
-
n
Integer
Example:
hoon.clear();
hoon.localStorage.setItem("name", "John");
console.log(hoon.localStorage.key(0)); // "name"
remove
-
keys
Parameters:
-
keys
Null | Array of String | String
Example:
hoon.localStorage.remove("name"); // equivalent to localStorage.removeItem("name");
hoon.localStorage.remove(["name", "id"]);
hoon.localStorage.remove(); // equivalent to localStorage.clear();
hoon.localStorage.remove(null); // equivalent to localStorage.clear();
removeItem
-
key
This method is equivalent to webstorage.removeItem(key)
.
Parameters:
-
key
String
Example:
hoon.localStorage.setItem("name", "John");
hoon.localStorage.removeItem("name");
set
()
Warning: The following methods must be used together.
- WebStorage.set()
- WebStorage.get()
- WebStorage.setItem()
- WebStorage.getItem()
For example, the following code doesn't work as expected.
localStorage.setItem("name", "John"); var name = hoon.localStorage.getItem("name", "John");
Otherwise, the following code works as expected.
hoon.localStorage.setItem("name", "John"); var name = hoon.localStorage.getItem("name", "John");
Example:
// equivalent to hoon.localStorage.setItem("name", "John");
hoon.localStorage.set("name", "John");
hoon.localStorage.set({
name: "John",
number: 1,
});
setItem
-
key
-
val
Warning: The following methods must be used together.
- WebStorage.set()
- WebStorage.get()
- WebStorage.setItem()
- WebStorage.getItem()
For example, the following code doesn't work as expected.
localStorage.setItem("name", "John"); var name = hoon.localStorage.getItem("name", "John");
Otherwise, the following code works as expected.
hoon.localStorage.setItem("name", "John"); var name = hoon.localStorage.getItem("name", "John");
Parameters:
-
key
String -
val
Object
Returns:
undefined
Example:
hoon.localStorage.setItem("obj", {
name: "John",
number: 1,
});
var obj = hoon.localStorage.getItem("obj");
console.log();