Game Development Reference
In-Depth Information
key(n) : It returns the name of the nth key in the list. The order of keys
is user-agent defined, but must be consistent within an object so long as
the number of keys doesn't change. (Thus, adding or removing a key may
change the order of the keys, but merely changing the value of an ex-
isting key must not.) If n is greater than or equal to the number of key/
value pairs in the object, then this method must return null. The suppor-
ted property names on a Storage object are the keys of each key/value
pair currently present in the list associated with the object.
length : It returns the number of key/value pairs currently present in the
list associated with the object.
Local storage
The local storage mechanism is accessed through a property of the global object,
which on browsers is the window object. Thus, we can access the storage property
explicitly through window.localStorage , or implicitly as simply localStorage .
window.localStorage.clear();
localStorage.length == 0; // True
Since only DOMString values are allowed to be stored in localStorage, any other val-
ues other than strings are converted into a string before being stored in localStorage.
That is, we can't store arrays, objects, functions, and so on in localStorage . Only
plain JavaScript strings are allowed.
var typedArray = new Uint32Array(100);
localStorage.setItem("my-array", typedArray);
var myArray = localStorage.getItem("my-array");
myArray == "[object Uint32Array]"; // True
Now, while this might seem like a limitation to the storage API, this is in fact done by
design. If your goal is to store complex data types for later use, localStorage wasn't
necessarily designed to solve this problem. In those situations, we have a much
more powerful and convenient storage solution, which we'll look at soon (that is, In-
dexedDB). However, there is a way to store complex data (including arrays, typed
arrays, objects, and so on) in localStorage.
Search WWH ::




Custom Search