Quite weird but interesting: if (+$foo){ // got elements }
:) On Jul 23, 7:38 pm, "Michael Geary" <m...@mg.to> wrote: > I'm with you there. > > You know how I love to simplify code. If I learned that you didn't have to > say .length to get the length, then the very next thing I'd do would be to > try to simplify this code that I write so often: > > var $foo = $('.foo'); > if( $foo.length ) { > // got some elements > } > > by changing it to: > > var $foo = $('.foo'); > if( $foo ) { > // got some elements > } > > :-( > > Ah well, nice idea, wasn't it. :-) > > -Mike > > p.s. Too bad there's no .booleanOf() or .toBoolean() method in JavaScript - > then we could do this right! > > _____ > > From: John Resig > > I'm hesitant to add that (I've definitely thought about it, in the past) > because .valueOf() doesn't cover all comparison cases. > > obj > obj2 works (as you noted) > but if ( obj ) {} doesn't (it always returns true) > > Additionally, in Firebug, the result shows up as a number rather than > something more useful (like a list of elements). > > --John > > On Thu, Jul 23, 2009 at 2:06 PM, James Padolsey > > <jamespadol...@googlemail.com> wrote: > > Just a thought; this would be quite useful IMO: > > var paras = $('p'), uls = $('ul'); > if (paras > uls) { > // ... > > } > > I know it's not quite as readable (or as semantic) as: > > if (paras.length > uls.length) { > // ... > > } > > but still, it's one of those things that may as well be added, just > for the few situations where it may be useful. > > jQuery.fn.valueOf = function(){return this.length;}; > > Unless it's got some other valid use (with jQuery)...? --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "jQuery Development" group. To post to this group, send email to jquery-dev@googlegroups.com To unsubscribe from this group, send email to jquery-dev+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/jquery-dev?hl=en -~----------~----~----~----~------~----~------~--~---