Skip to content

Instantly share code, notes, and snippets.

@padolsey
Created December 16, 2013 17:08
Show Gist options
  • Save padolsey/7990476 to your computer and use it in GitHub Desktop.
Save padolsey/7990476 to your computer and use it in GitHub Desktop.
Get notified when you're attempting jQuery method calls on an empty collection
for (var i in $.fn) (function(method, name) {
// ignore traversal/selection methods
if (typeof method != 'function' || [
'constructor', 'init', 'find', 'add', 'next', 'nextUnil', 'prevUntil',
'parents', 'closest', 'eq', 'filter', 'has', 'first', 'siblings',
'last', 'map', 'not', 'slice', 'addBack', 'andSelf', 'contents', 'prev',
'end', 'not', 'children', 'nextAll', 'prevAll', 'parent', 'parentsUntil'
].indexOf(name) > -1) {
return;
}
$.fn[name] = function() {
if (this.length === 0) {
console.warn('Trying to call ' + name + '() on an empty collection: ' + this.selector);
}
return method.apply(this, arguments);
};
}($.fn[i], i));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment