Close Menu
geekfence.comgeekfence.com
    What's Hot

    AI will likely shut down critical infrastructure on its own, no attackers required – Computerworld

    February 15, 2026

    A new era of humanoid robots

    February 15, 2026

    LLaMA in R with Keras and TensorFlow

    February 15, 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

    An Approach to Accelerate Verification and Software Standards Testing with LLMs

    February 14, 2026

    Gas Town, Beads, and the Rise of Agentic Development with Steve Yegge

    February 13, 2026

    Artificial Intelligence in National Security: Acquisition and Integration

    February 9, 2026

    Airbnb’s Open-Source GraphQL Framework with Adam Miskiewicz

    February 8, 2026

    Upskilling the Federal Cybersecurity Workforce

    February 4, 2026

    SED News: Apple Bets on Gemini, Google’s AI Advantage, and the Talent Arms Race

    February 3, 2026
    Top Posts

    Hard-braking events as indicators of road segment crash risk

    January 14, 202617 Views

    Understanding U-Net Architecture in Deep Learning

    November 25, 202512 Views

    How to integrate a graph database into your RAG pipeline

    February 8, 20268 Views
    Don't Miss

    AI will likely shut down critical infrastructure on its own, no attackers required – Computerworld

    February 15, 2026

    With a new Gartner report suggesting that AI problems will “shut down national critical infrastructure”…

    A new era of humanoid robots

    February 15, 2026

    LLaMA in R with Keras and TensorFlow

    February 15, 2026

    How Deutsche Börse Federates Data Governance with Control

    February 15, 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

    AI will likely shut down critical infrastructure on its own, no attackers required – Computerworld

    February 15, 2026

    A new era of humanoid robots

    February 15, 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.