Close Menu
geekfence.comgeekfence.com
    What's Hot

    MCP is not another AdTech buzzword – it’s the workflow layer AI has been missing 

    April 2, 2026

    T-Mobile and TPG eye Uniti’s fibre assets

    April 2, 2026

    Evaluating the ethics of autonomous systems | MIT News

    April 2, 2026
    Facebook X (Twitter) Instagram
    • About Us
    • Contact Us
    Facebook Instagram
    geekfence.comgeekfence.com
    • Home
    • UK Tech News
    • AI
    • Big Data
    • Cyber Security
      • Cloud Computing
      • iOS Development
    • IoT
    • Mobile
    • Software
      • Software Development
      • Software Engineering
    • Technology
      • Green Technology
      • Nanotechnology
    • Telecom
    geekfence.comgeekfence.com
    Home»Software Engineering»How to Override Methods in localStorage? | by Sabesan Sathananthan
    Software Engineering

    How to Override Methods in localStorage? | by Sabesan Sathananthan

    AdminBy AdminNovember 10, 2025No Comments3 Mins Read6 Views
    Facebook Twitter Pinterest LinkedIn Telegram Tumblr Email
    How to Override Methods in localStorage? | by Sabesan Sathananthan
    Share
    Facebook Twitter LinkedIn Pinterest Email


    Use localStorage with JavaScript.

    Sabesan Sathananthan

    Press enter or click to view image in full size

    Photo by Lia Trevarthen on Unsplash

    I often want to rewrite localStorage to implement a certain function. What are the methods to rewrite the methods in localStorage? There are many developers who want to rewrite the methods in localStorageto realize the expiration time of the key, or to monitor the read and write of the key. So what are the methods to override the methods in localStorage. This is my 43rd Medium article.

    1. Rewrite directly on localStorage

    Many developers like the idea of ​​rewriting, first keeping the original method and then rewriting the method directly on localStorage like below.

    However, this way of writing is not overriding the method setItem(), but to add a setItem attribute to localStorage. When the value attribute of the method is declared, the native setItem() method is overwritten.

    I haven’t tested it too much, but in some browsers, this attribute will be ignored and causing our rewriting to fail.

    2. Override the method on localStorage.__proto__

    If we look closely, setItem and getItem are inherited from Storage __proto__ pseudo property.

    Press enter or click to view image in full size

    Then we directly override the above localStorage.__proto__ method.

    This implements the real override of the setItem() method.

    But there is still a problem here. Both localStorage and sessionStorage inherit from Storage. After rewriting the properties or methods on localStorage.__proto__, the methods in sessionStorage are also rewritten.

    Press enter or click to view image in full size

    3. External packaging layer

    We do not directly modify the method of localStorage itself, but wrap a layer on the outside, and then use localStorage to realize the storage function at the bottom layer.

    In this way, the degree of freedom is relatively higher, and there is no compatibility problem in Section 1. Only the name used has changed and the properties and methods in localStorage are completely blocked.

    If you want to use a custom object without the pack then you need to implement all the properties and methods. It is not possible to mock a method alone like the above.

    4. Override localStorage

    Use Object.definePropertyor Proxyequivalent to completely overriding the localStorage variable. Better than Section 3 in that the name has not changed.

    4.1 Direct coverage, no effect

    If you use the following method to cover directly, it will have no effect.

    window.localStorage = Object.create(null);  console.log(window.localStorage); //still native

    We get the property descriptor of localStorage through Object.getOwnPropertyDescriptor. It can be found that there is no writable: true attribute, which means that localStorage is not directly writable.

    Press enter or click to view image in full size

    4.2 Overriding with Object.defineProperty

    Since there is no writableattribute, we will add one to it. We can override localStorage with Object.defineProperty.

    But you can’t use the above writing method with one layer outside. If you directly give the above myLocalStorage to localStorage then it will generate infinite recursion (to avoid misleading, the wrong writing method will not be written here).

    I have made a backup of localStorage here. If you need a native method then you can also operate it.

    5. Summary

    In this article, we do not specifically implement a function such as setting the expiration time. But talk about how to rewrite localStorage or the methods in it from another perspective.

    More content at PlainEnglish.io. Sign up for our free weekly newsletter. Follow us on Twitter, LinkedIn, YouTube, and Discord.





    Source link

    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email

    Related Posts

    Which Agile Leadership Archetype Are You? Take the quiz

    April 2, 2026

    FreeBSD with John Baldwin – Software Engineering Daily

    April 1, 2026

    The Impact of 3DGS on Training, Education, and Beyond

    March 28, 2026

    Cilium, eBPF, and Modern Kubernetes Networking with Bill Mulligan

    March 27, 2026

    A Day in the Life of an Engineering Manager

    March 26, 2026

    Dan Lorenc on Sigstore – Software Engineering Radio

    March 23, 2026
    Top Posts

    Understanding U-Net Architecture in Deep Learning

    November 25, 202527 Views

    Hard-braking events as indicators of road segment crash risk

    January 14, 202624 Views

    Redefining AI efficiency with extreme compression

    March 25, 202622 Views
    Don't Miss

    MCP is not another AdTech buzzword – it’s the workflow layer AI has been missing 

    April 2, 2026

      Advertising has automated transactions, but not workflows. Bidding, targeting, and optimization are already machine-led…

    T-Mobile and TPG eye Uniti’s fibre assets

    April 2, 2026

    Evaluating the ethics of autonomous systems | MIT News

    April 2, 2026

    Navigating multi-account deployments in Amazon SageMaker Unified Studio: a governance-first approach

    April 2, 2026
    Stay In Touch
    • Facebook
    • Instagram
    About Us

    At GeekFence, we are a team of tech-enthusiasts, industry watchers and content creators who believe that technology isn’t just about gadgets—it’s about how innovation transforms our lives, work and society. We’ve come together to build a place where readers, thinkers and industry insiders can converge to explore what’s next in tech.

    Our Picks

    MCP is not another AdTech buzzword – it’s the workflow layer AI has been missing 

    April 2, 2026

    T-Mobile and TPG eye Uniti’s fibre assets

    April 2, 2026

    Subscribe to Updates

    Please enable JavaScript in your browser to complete this form.
    Loading
    • About Us
    • Contact Us
    • Disclaimer
    • Privacy Policy
    • Terms and Conditions
    © 2026 Geekfence.All Rigt Reserved.

    Type above and press Enter to search. Press Esc to cancel.