Skip to content

Instantly share code, notes, and snippets.

@tsvensen
Created September 5, 2014 22:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tsvensen/4efedfe486ecbc4d3345 to your computer and use it in GitHub Desktop.
Save tsvensen/4efedfe486ecbc4d3345 to your computer and use it in GitHub Desktop.
jQuery Element Caching System
// Requires underscore or lodash for isUndefined()
var $cache = {};
/**
* Returns the page title element and will set the page title element when it doesn't exist
* @param {String} selector
* @returns {jQuery}
*/
getElement: function getElement(selector) {
if (typeof selector !== 'string') {
throw new Error('Selector must be a string');
}
return (_.isUndefined($cache[selector])) ? $cache[selector] = $(selector) : $cache[selector];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment