Skip to content

Instantly share code, notes, and snippets.

Created December 19, 2012 00:09
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/4333310 to your computer and use it in GitHub Desktop.
Save anonymous/4333310 to your computer and use it in GitHub Desktop.
(function(root) { // Wrap our entire code in anyonmous function. Minimise your chance to leak scope into the global namespace. See last line.
'use strict'; // http://stackoverflow.com/questions/1335851/what-does-use-strict-do-in-javascript-and-what-is-the-reasoning-behind-it
var $ = root.$; // Convient access to the jQuery object. Chaining is expensive in JS.
root.Search = (function() { // Wrap the entire class in an anonymous function. This guarantees we will not leak scope. We assign the class to the root object. In this case, window.
function Search(el) { // Create a search object. This is also the constructor for the class.
this.el = el;// Assign an ivar
}
Search.prototype.perform = function() { // Instance method
// $ do something
}
return Search // Return the search object
})();
$(root.document).on('keyup', '#search', function() {
new root.Search(this).perform();
});
}).call(this); // `this` in a global context is always the root object. In the browser, it's window.
test('perform works as intended', function() {
var search = new Search();
ok(search);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment