[jquery-dev] Re: Having valueOf() reflect length of collection - a good idea?

2009-07-23 Thread Ricardo
Quite weird but interesting: if (+$foo){ // got elements } :) On Jul 23, 7:38 pm, "Michael Geary" 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

[jquery-dev] Re: Having valueOf() reflect length of collection - a good idea?

2009-07-23 Thread Michael Geary
"!! obj" and "if(obj)" don't call .valueOf() at all. _ From: John Resig Nope. var obj = { valueOf: function(){ return 0; } }; !!obj >> true obj.valueOf().constructor >> Number() It looks like it's returning the number as Number(0) rather than in its primitive form. --John On Thu

[jquery-dev] Re: Having valueOf() reflect length of collection - a good idea?

2009-07-23 Thread Daniel Friesen
O_o wtf, I thought that valueOf was ignored for those kind of bools. Taking another look, !! doesn't even run valueOf as I thought. My comment was on the > operator, and your comment about what jquery shows up as in firebug if used. jQuery.fn.valueOf = function(hint){ if ( hint === "number

[jquery-dev] Re: Having valueOf() reflect length of collection - a good idea?

2009-07-23 Thread John Resig
Nope. var obj = { valueOf: function(){ return 0; } }; !!obj >> true obj.valueOf().constructor >> Number() It looks like it's returning the number as Number(0) rather than in its primitive form. --John On Thu, Jul 23, 2009 at 5:49 PM, Daniel Friesen wrote: > > John Resig wrote: > > I'm hesita

[jquery-dev] Re: Having valueOf() reflect length of collection - a good idea?

2009-07-23 Thread Michael Geary
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 elemen

[jquery-dev] Re: Having valueOf() reflect length of collection - a good idea?

2009-07-23 Thread Daniel Friesen
John Resig wrote: > 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 num

[jquery-dev] Re: Having valueOf() reflect length of collection - a good idea?

2009-07-23 Thread 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 u