Close Menu
geekfence.comgeekfence.com
    What's Hot

    Luna Ring Gen 2 Review: Stylish and Subscription-Free

    December 29, 2025

    HCLTech acquires HPE telco unit

    December 29, 2025

    This tiny chip could change the future of quantum computing

    December 29, 2025
    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 Read0 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

    Supercharge your business with software modernization

    December 28, 2025

    Spillover in Agile: 3 Ways to Break an Unfinished Work Habit

    December 26, 2025

    Enhancing Security with Cloud Flow Logs

    December 25, 2025

    Node.js in 2026 with Rafael Gonzaga

    December 24, 2025

    Evaluating SaaS and SaaP for your business model

    December 22, 2025

    Why Teams Matter More Than Ever for Innovation

    December 20, 2025
    Top Posts

    Understanding U-Net Architecture in Deep Learning

    November 25, 20258 Views

    Microsoft 365 Copilot now enables you to build apps and workflows

    October 29, 20258 Views

    Here’s the latest company planning for gene-edited babies

    November 2, 20257 Views
    Don't Miss

    Luna Ring Gen 2 Review: Stylish and Subscription-Free

    December 29, 2025

    At a glanceExpert’s Rating Pros Likeable design Sleek companion app Good sleep tracking Choice of…

    HCLTech acquires HPE telco unit

    December 29, 2025

    This tiny chip could change the future of quantum computing

    December 29, 2025

    What’s In a Name? Mainframe GDGs Get the Job Done

    December 29, 2025
    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

    Luna Ring Gen 2 Review: Stylish and Subscription-Free

    December 29, 2025

    HCLTech acquires HPE telco unit

    December 29, 2025

    Subscribe to Updates

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

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