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:
-
webstorageLocalStorage | 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:
-
keysNull | 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:
-
keyString
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:
-
nInteger
Example:
hoon.clear();
hoon.localStorage.setItem("name", "John");
console.log(hoon.localStorage.key(0)); // "name"
remove
-
keys
Parameters:
-
keysNull | 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:
-
keyString
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:
-
keyString -
valObject
Returns:
undefined
Example:
hoon.localStorage.setItem("obj", {
name: "John",
number: 1,
});
var obj = hoon.localStorage.getItem("obj");
console.log();
