javascriptlocal-storagesession-storageweb-storage

Why can't I obtain a memory-backed implementation of the Storage interface from the Web Storage API?


Refs: Spec, MDN

If I wanted an object that implements the API of the Storage interface — but not for the persistence (backed only by memory):

I understand that I could re-implement it on my own using a custom class, backed by a string Map (example) — but is there some objective, technical reason that there's no way to simply create one? Basically: all of the code to do it is already there — why do I need to implement my own?

const genericStorage = new Storage(); // throws TypeError (expected)

To be clear: I'm not asking why the code example above throws a TypeError or why Storage isn't constructible according to the spec.


Solution

  • The "objective, technical reason" (as explained in the comments) is that Storage is an abstract interface and can't be instantiated.

    To reframe this: what would be the "objective, technical reason" for a new Storage() to be backed by transient memory? If you ask me, nothing – that'd be a MemoryStorage.