Does jQuery have any "find or fail loudly" methods? -
consider following jquery code:
$('.child').closest('.parent').find('.sibling').hide();
at every step, if nothing found, jquery fail silently (for example, if there no .parent
).
sometimes makes sense, means code nothing , have troubleshoot why. i'd option like:
$('.child').closest!('.parent')...
meaning "fail loudly if don't find this". (bonus if enabled when developing.)
is there in jquery?
there's none, make easily:
object.keys($.fn).foreach(function(k){ var f = $.fn[k]; if (typeof f !=='function') return; $.fn['_'+k] = function(){ var o = f.apply(this, arguments); if (o && 'length' in o && !o.length) throw "loud"; return o; } }) $("body")._find("unexisting"); // throws loud
alternatively, replace existing functions when in debug mode:
if (debug_mode) { // let's more cautious explicit list time ['find','closest]'].foreach(function(k){ var f = $.fn[k]; $.fn[k] = function(){ var o = f.apply(this, arguments); if (o && 'length' in o && !o.length) { throw "loud"; // shall make more interesting error } return o; }; }); }
possibilities endless jquery prototype exposed.
now...the fact jquery doesn't fail on empty collections useful. of course can decide integrate code debug/dev session.
Comments
Post a Comment