Game Development Reference
In-Depth Information
};
};
// Now there exists a property of window that
behaves just like
// one would expect the local storage object
to (although in this example
// the functionality is reduced in order to
make the point)
window.localStorage = new FauxStorage();
}
// This code will work just fine whether or not
the browser supports the real
// HTML5 API for local storage. No exceptions
will be thrown.
localStorage.setItem("name", "Rodrigo
Silveira");
localStorage.length == 1; // true
localStorage.getItem("name"); // "Rodrigo
Silveira"
localStorage.removeItem("name");
localStorage.length; // == 0
Although the preceding polyfill really doesn't store any data beyond the current ses-
sion, this particular implementation of a local storage polyfill can be enough for the
needs of a given application. At the very least, this implementation allows us to code
to the official interface (calling the real methods defined by the specification), and no
exceptions are thrown by the browser, since the methods are indeed present. Even-
tually, whenever the browser that didn't support the HTML5 API, and thus used our
polyfill because of the conditional that checked for browser support of the feature,
that conditional will no longer trigger the polyfill to be loaded, thus the client code
will always refer to the original implementation and no changes will be needed to the
main source code.
While it is quite exciting to consider what polyfills can do for us, the observant student
will quickly notice that writing complete, secure, and accurate polyfills is slightly more
complicated than adding simple CSS hacks to a style sheet in order to make a
Search WWH ::




Custom Search