最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

local storage - Overriding default functions in Javascript? - Stack Overflow

programmeradmin3浏览0评论

Question:
Can I override "default" functions in Javascript?

Background:
After figuring out that I had collisions between objects stored in localStorage, I decided that I should apply a prefix to all keys to avoid collisions. Obviously, I could create a wrapper function, but it would be so much neater to override the default localStorage.getItem & localStorage.setItem directly to take my prefix into account.

My example kills Firefox pletely as it recursively calls itself, so it clearly isn't close to a solution. Perhaps it clarifies what I want to acplish though.

Code:

Storage.prototype.setItem = function(key, value) {
    this.setItem("prefix"+key, value);
};

Storage.prototype.getItem = function(key, value) {
    return this.getItem("prefix"+key);
};

Question:
Can I override "default" functions in Javascript?

Background:
After figuring out that I had collisions between objects stored in localStorage, I decided that I should apply a prefix to all keys to avoid collisions. Obviously, I could create a wrapper function, but it would be so much neater to override the default localStorage.getItem & localStorage.setItem directly to take my prefix into account.

My example kills Firefox pletely as it recursively calls itself, so it clearly isn't close to a solution. Perhaps it clarifies what I want to acplish though.

Code:

Storage.prototype.setItem = function(key, value) {
    this.setItem("prefix"+key, value);
};

Storage.prototype.getItem = function(key, value) {
    return this.getItem("prefix"+key);
};
Share Improve this question asked Feb 13, 2012 at 21:40 IndustrialIndustrial 42.8k71 gold badges201 silver badges292 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 12

You need to store the old function.

Storage.prototype._setItem = Storage.prototype.setItem;
Storage.prototype.setItem = function(key, value) {
    this._setItem("prefix" + key, value);
};

Storage.prototype._getItem = Storage.prototype.getItem;
Storage.prototype.getItem = function(key) {
    return this._getItem("prefix" + key);
};

If you don't, you get an infinite loop consuming stack space at every iteration, resulting in a stack overflow, crashing your browser :)

Alternatively, instead of creating a new variables to hold the old Storage functions you could always bind your functions like so.

Storage.prototype.setItem = (function(key, value) {
    this.call(localStorage,"prefix" + key, value);
}).bind(Storage.prototype.setItem);

Storage.prototype.getItem = (function(key) {
    return this.call(localStorage,"prefix" + key);
}).bind(Storage.prototype.getItem);

And you get the benefits of representing your new functions as native code when inspected in the console as well as a less cluttered code.

That is normal, you make a infinite recursion : in Storage.prototype.setItem, you call this.setItem that refers to Storage.prototype.setItem.

The same for Storage.prototype.getItem.

发布评论

评论列表(0)

  1. 暂无评论