I typically wish to rewrite localStorage
to implement a sure perform. What are the strategies to rewrite the strategies in localStorage
? There are a lot of builders who wish to rewrite the strategies in localStorage
to comprehend the expiration time of the important thing, or to observe the learn and write of the important thing. So what are the strategies to override the strategies in localStorage
. That is my forty third Medium article.
Many builders like the concept of rewriting, first holding the unique methodology after which rewriting the tactic straight on localStorage like beneath.
Nonetheless, this manner of writing just isn’t overriding the tactic setItem()
, however so as to add a setItem
attribute to localStorage
. When the worth attribute of the tactic is said, the native setItem()
methodology is overwritten.
I haven’t examined it an excessive amount of, however in some browsers, this attribute will likely be ignored and inflicting our rewriting to fail.
If we glance intently, setItem and getItem are inherited from Storage __proto__
pseudo property.
Then we straight override the above localStorage.__proto__
methodology.
This implements the true override of the setItem()
methodology.
However there may be nonetheless an issue right here. Each localStorage
and sessionStorage
inherit from Storage. After rewriting the properties or strategies on localStorage.__proto__
, the strategies in sessionStorage
are additionally rewritten.
We don’t straight modify the tactic of localStorage
itself, however wrap a layer on the surface, after which use localStorage
to comprehend the storage perform on the backside layer.
On this method, the diploma of freedom is comparatively increased, and there’s no compatibility drawback in Part 1. Solely the title used has modified and the properties and strategies in localStorage
are utterly blocked.
If you wish to use a customized object with out the pack then you might want to implement all of the properties and strategies. It isn’t attainable to mock a way alone just like the above.
Use Object.defineProperty
or Proxy
equal to utterly overriding the localStorage
variable. Higher than Part 3 in that the title has not modified.
4.1 Direct protection, no impact
In case you use the next methodology to cowl straight, it can haven’t any impact.
window.localStorage = Object.create(null); console.log(window.localStorage); //nonetheless native
We get the property descriptor of localStorage
by way of Object.getOwnPropertyDescriptor
. It may be discovered that there is no such thing as a writable: true attribute, which signifies that localStorage
just isn’t straight writable.
4.2 Overriding with Object.defineProperty
Since there is no such thing as a writable
attribute, we are going to add one to it. We will override localStorage
with Object.defineProperty
.
However you’ll be able to’t use the above writing methodology with one layer outdoors. In case you straight give the above myLocalStorage
to localStorage
then it can generate infinite recursion (to keep away from deceptive, the flawed writing methodology is not going to be written right here).
I’ve made a backup of localStorage
right here. In case you want a local methodology then you can too function it.
On this article, we don’t particularly implement a perform reminiscent of setting the expiration time. However speak about learn how to rewrite localStorage
or the strategies in it from one other perspective.
Extra content material at PlainEnglish.io. Join our free weekly e-newsletter. Comply with us on Twitter, LinkedIn, YouTube, and Discord.