HTML and CSS Reference
In-Depth Information
Using a jQuery Alternative Such As Zepto.js.
jQuery's greatest strength is that it unifies the programmer experience across all different browsers from IE6 for-
ward. Its greatest strength, however, also leads to two great weaknesses: It's large and can be slow. Zepto.js is a
library that was created by JavaScript wizard Thomas Fuchs to provide a jQuery-like syntax without the bloat of
For the most part, Zepto.js is a drop-in replacement focused on modern browsers other than Internet Explorer
(IE). Because it has a razor-sharp focus on supporting WebKit-based and Firefox browsers, the current minified
JavaScript code is under 6 kb and works great on almost all mobile devices (WP7 being the exception). Zepto.js
does not work on IE, including IE9, so if you target desktop browsers for your game as well, jQuery is still the
better option.
If you want to keep your loading and your file size down, and still get the convenience of jQuery, look at Zepto.js
at .
Creating Callbacks
One of the most significant features of JavaScript that developers from other languages often have trouble with
is the idea of functions as first-class objects. Although most other languages support callback mechanisms, the
ubiquity of callbacks and the passing around of functions as parameters make JavaScript a bit different.
As you know, functions in JavaScript are created with the function() keyword. Functions can have
names, for example:
function sayPhrase() { ... }
Or they can be anonymous:
function() { ... }
What good are anonymous functions? Well, as mentioned, they can be passed in parameters and treated like
normal objects. If you want to save your anonymous function for later, you could write:
var sayPhrase = function() { ... }
Doing so also drives home the point that functions can be treated like normal objects and can be assigned to
variables like any other value. (This topic mostly shows this later form to make it apparent that functions get
passed around just like any other object.)
To call sayPhrase you need to append parentheses to the name, but if you don't want to call the function
and instead just want to pass it to another function as a callback, leave the parentheses off.
sayPhrase(); // Call sayPhrase
// Pass sayPhrase as a callback to another function
When you understand functions as first-class objects in jQuery, there's a second subtlety related to calling
functions. JavaScript, as you know, is an object-oriented (OO) language, albeit not a typical one. Objects are
data structures that combine data with methods for interacting with that data.
A standard, object-based method call in JavaScript looks like the following:
Search WWH ::

Custom Search