Localstorage please use GetItem and SetItem

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.