Thursday, June 20, 2024

Override Strategies in localStorage? | by Sabesan Sathananthan


Use localStorage with JavaScript.

Photograph by Lia Trevarthen on Unsplash

I usually need to rewrite localStorage to implement a sure operate. What are the strategies to rewrite the strategies in localStorage? There are lots of builders who need to rewrite the strategies in localStorageto comprehend the expiration time of the important thing, or to watch 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 thought of ​​rewriting, first retaining the unique methodology after which rewriting the tactic instantly on localStorage like beneath.

Nonetheless, this manner of writing will not be 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 shall be ignored and inflicting our rewriting to fail.

If we glance intently, setItem and getItem are inherited from Storage __proto__ pseudo property.

Then we instantly override the above localStorage.__proto__ methodology.

This implements the true override of the setItem() methodology.

However there’s 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 instantly modify the tactic of localStorage itself, however wrap a layer on the surface, after which use localStorage to comprehend the storage operate on the backside layer.

On this means, the diploma of freedom is comparatively larger, 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 it’s worthwhile to implement all of the properties and strategies. It isn’t attainable to mock a way alone just like the above.

Use Object.definePropertyor Proxyequal to utterly overriding the localStorage variable. Higher than Part 3 in that the title has not modified.

4.1 Direct protection, no impact

Should you use the next methodology to cowl instantly, it’ll haven’t any impact.

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

We get the property descriptor of localStorage via Object.getOwnPropertyDescriptor. It may be discovered that there isn’t any writable: true attribute, which signifies that localStorage will not be instantly writable.

4.2 Overriding with Object.defineProperty

Since there isn’t any writableattribute, we are going to add one to it. We are able to override localStorage with Object.defineProperty.

However you may’t use the above writing methodology with one layer outdoors. Should you instantly give the above myLocalStorage to localStorage then it’ll generate infinite recursion (to keep away from deceptive, the unsuitable writing methodology won’t be written right here).

I’ve made a backup of localStorage right here. Should you want a local methodology then you may as well function it.

On this article, we don’t particularly implement a operate corresponding to setting the expiration time. However discuss how one can rewrite localStorage or the strategies in it from one other perspective.

Extra content material at PlainEnglish.io. Join our free weekly publication. Comply with us on Twitter, LinkedIn, YouTube, and Discord.



Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Stay Connected

0FansLike
3,912FollowersFollow
0SubscribersSubscribe
- Advertisement -spot_img

Latest Articles