Game Development Reference
In-Depth Information
A simple solution to a problem such as this is to use polyfills, which we'll discuss
more in depth next. In short, though, a polyfill is a JavaScript alternative that the
browser can use when the original implementation is not yet available. This way, you
can load the polyfill if the browser needs it, and the rest of the code base can use the
functionality through the original interface, and never know which implementation it
is working with. In the case of localStorage , we could simply check whether the
authentic API is available, and write code that mimics its behavior if it is not avail-
able. The following code snippet shows this behavior:
// If the browser doesn't know anything about
localStorage,
// we create our own, or at least an interface
that respond
// to the calls we'd make to the real storage
object.
if (window.localStorage === undefined) {
var FauxLocalStorage = function() {
var items = {};
this.length = 0;
this.setItem = function(key, value) {
items[key] = value;
this.length++;
};
this.getItem = function(key) {
if (items[key] === undefined)
return undefined;
return items[key];
};
this.removeItem = function(key) {
if (items[key] === undefined)
return undefined;
this.length--;
return delete items[key];
Search WWH ::




Custom Search