Created
December 16, 2013 17:08
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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