Throughout the web I've seen 3 approaches to save and retrieve data from localStorage
:
//As an array
localStorage["key"] = "value";
var value = localStorage[key];
//As an object, using properties
localStorage.key = value;
var value = localStorage.key;
//As an object, using getter and setter functions
localStorage.setItem("key", "value");
var value = localStorage.getItem("key");
From what I know the first two are equivalent, as an array in JavaScript is treated like an object.
The third one seems the best approach because using getters and setters enables encapsulation of the function's logic, and also extension.
I'm hoping to get some more insight on this, can someone advise?
EDIT: The reason for this question comes from the fact that localStorage is more than just an array so I'm looking for opinions explicitly targeting localStorage and it's implementation.