This is a small jQuery plugin I use for getting console.warn when there is no selector returned from a query
(function($, w, c){
if(!w.jQuery || !c) return;
c.w = c.warn || c.log; // safely use either warn or log
// jQuery and console.warn/log are available, we're good to go
var _find = $.find,
_attr = $.fn.attr;
// 'duck punch' jQuery.find - Replace with a wrapper function with our warning which returns the original result
$.find = function(){
var result = _find.apply(this, arguments);
if(!result.length) c.w('jQuery Selector "' + result.selector + '" returned no matches');
return result;
};
$.fn.attr = function(attr){
if(arguments.length === 1) {
var result = _attr.call(this, attr);
if(result === void 0) {
c.w('jQuery Attribute Getter for "' + attr + '" returned undefined for selector "' + $(this).selector + '"');
}
return result;
} else {
_attr.apply(this, arguments);
return this;
}
};
w.jQuery = w.$;
}(jQuery, window, console));
And what I get in the console when there is an empty selector is this:
Uncaught TypeError: Object function (){
var result = _find.apply(this, arguments);
if(!result.length) c.w('jQuery Selector "' + result.selector + '" returned no matches');
return result;
} has no method 'matchesSelector'
I don’t understand what is matchesSelector and where it comes from
You are overwriting
$.findbut not replacing the properties jQuery is relying on.$.findhas many properties, such asmatchesSelector,matchesetc which are lost because they have not been assigned to your function.You should not overwrite
$.findYou can attach them back but I have a bad feeling some other problems are still missed:
There are hardcoded references to
$.findproperties all over jQuery source, such as this one:Result of filling a normal object with those: