add small code tweaks
[2048.git] / js / local_score_manager.js
blob:a/js/local_score_manager.js -> blob:b/js/local_score_manager.js
window.fakeStorage = { window.fakeStorage = {
_data : {}, _data : {},
setItem : function (id, val) { setItem : function (id, val) {
console.log('set'); console.log("set");
return this._data[id] = String(val); return this._data[id] = String(val);
}, },
getItem : function (id) { getItem : function (id) {
return this._data.hasOwnProperty(id) ? this._data[id] : undefined; return this._data.hasOwnProperty(id) ? this._data[id] : undefined;
}, },
removeItem : function (id) { return delete this._data[id]; }, removeItem : function (id) { return delete this._data[id]; },
clear : function () { return this._data = {}; } clear : function () { return this._data = {}; }
}; };
   
function LocalScoreManager() { function LocalScoreManager() {
var localSupported = !!window.localStorage; var localSupported = !!window.localStorage;
this.key = 'bestScore';  
this.storage = localSupported ? window.localStorage : window.fakeStorage; this.key = "bestScore";
  this.storage = localSupported ? window.localStorage : window.fakeStorage;
} }
   
LocalScoreManager.prototype.get = function () { LocalScoreManager.prototype.get = function () {
var score = this.storage.getItem(this.key); var score = this.storage.getItem(this.key);
if (typeof score === "undefined" || score === null) { if (typeof score === "undefined" || score === null) {
score = 0; score = 0;
} }
return score; return score;
}; };
   
LocalScoreManager.prototype.set = function (score) { LocalScoreManager.prototype.set = function (score) {
this.storage.setItem(this.key, score); this.storage.setItem(this.key, score);
}; };
   
   
comments