Recently look at other people's code, found that they like to use Dot to operate from inside the localstorage, rather than get setitem, remember said this before. Let's say it again.
Use dot mode (each time with ' hello ' = ' world ', ' zhangsan ' = ' lisi ' as an example)
' Lisi '; value:var value_of_hello_in_localstorage = Localstorage.hello; var value_of_zhangsan_in_localstorage = Localstorage.zhangsan
Use GetItem SetItem to operate
Set Value: Localstorage.setitem ("Hello", "World"), Localstorage.setitem ("Zhangsan", "Lisi"), Value: Localstorage.getitem (" Hello "); Localstorage.getitem (" Zhangsan ");
The efficiency of these two methods is not clear to me, but why is it recommended to use get SetItem?
1. Easy to control, easy to bind dynamically, especially in function extraction and reconstruction. For example, I want to save a complex type, every time I need to do json.stringify (object_a), so I put it into the function
Use the Dot method to set the value:storage_object (object) { Localstorage.object = Json.stringify (object);} //implemented in GetItem Way:(object) {Localstorage.setitem(object,json.stringify(object));}
At this time it will be obvious to find that the dot method is very inappropriate. Because he can't implement dynamic binding key.
2. Feel more beautiful with getitem, easy to read and understand. This is a personal opinion. I remember reading a piece of code, the effect is probably the implementation of the count it:
locatstorage.count++
When I saw him, it was a circle. It's quite simple, but it's not easy to understand. This is the test of my AH.
That's probably it.
Localstorage please use GetItem and SetItem