Ian Davis
Ian Davis

Reputation: 19423

localStorage.prototype is null

I'm trying to extend localStorage w/ the following...

localStorage.prototype.setItem2 = function(key,value) {
    localStorage.setItem(key,value);
}

I'm getting "localStorage.prototype is null." Am I doing this correctly? Thanks!

Upvotes: 8

Views: 2074

Answers (3)

Venkat.R
Venkat.R

Reputation: 7746

LocalStorage and sessionStorage Objects implements from Storage Interface.

You can extend the Storage through prototype.

Storage.prototype.removeItems = function () {
  for(item in arguments) {
    this.removeItem(arguments[item]);
  }
};

Refer to Article: HTML5: Storage APIs

Upvotes: 1

pradeek
pradeek

Reputation: 22105

You can directly set it by :

localStorage.setItem2 = function(key, value) {
    // do something
}

or use Storage.prototype

Before you do so, make sure you are not overwriting any existing property. This is to prevent any overwriting for future enhancements to the API by the browsers.

Upvotes: 1

Jim Deville
Jim Deville

Reputation: 10662

localStorage is an instance of the Storage object. Try Storage.prototype.setItem2 or Object.getPrototypeOf(localStorage).setItem2

Upvotes: 10

Related Questions