Re: [jQuery] .each backwards ?
> I agree, do you think .sort() and .reverse() could be added to core jQuery > object in the future, or should I just add a plugin. Sort, reverse, and splice are definitely possible - maybe for the 1.1 release. --John ___ jQuery mailing list discuss@jquery.com http://jquery.com/discuss/
Re: [jQuery] .each backwards ?
Oops, I meant to make that: jQuery.fn.unshift = jQuery.fn.add; The issue is, however, that fundamentally .push() or .unshift() won't work as expected, since adding an item to a jQuery object isn't like adding a item to a normal array. The jQuery object is more like a 'Set' than it is a true 'Array'. --John On 10/7/06, Jörn Zaefferer <[EMAIL PROTECTED]> wrote: > Hi John! > > jQuery.fn.unshift = jQuery.fn.unshift; > > > What is that supposed to do? > > -- Jörn > > ___ > jQuery mailing list > discuss@jquery.com > http://jquery.com/discuss/ > -- John Resig http://ejohn.org/ [EMAIL PROTECTED] ___ jQuery mailing list discuss@jquery.com http://jquery.com/discuss/
Re: [jQuery] .each backwards ?
On 10/7/06, Michael Geary <[EMAIL PROTECTED]> wrote: > > > jQuery.fn.reverse = function() { > > >this.pushStack(this.get().reverse()); > > >return this; > > > } > > > Nice. That is a better approach then just doing > > jQuery.fn.reverse = [].reverse. > > I'm curious, what is the advantage of one approach over the other? Since the first once uses jQuery's nice pushStack method, you can now do (in a silly example): $("div") .reverse() .addClass("test") .end() .find("p") or even: $("div").reverse(function(){ alert("All the divs, in reverse: " + this); }); --John ___ jQuery mailing list discuss@jquery.com http://jquery.com/discuss/
Re: [jQuery] .each backwards ?
> > jQuery.fn.reverse = function() { > >this.pushStack(this.get().reverse()); > >return this; > > } > Nice. That is a better approach then just doing > jQuery.fn.reverse = [].reverse. I'm curious, what is the advantage of one approach over the other? -Mike ___ jQuery mailing list discuss@jquery.com http://jquery.com/discuss/
Re: [jQuery] .each backwards ?
> > From: Michael Geary > > > > Armed with this knowledge, one might be tempted to load > > all the Array methods in one fell swoop: > > > >jQuery.fn.prototype = Array.prototype; > > > > But that does not work, presumably because jQuery is > > already being a bit sneaky about its Array-like behavior. > From: Jörn Zaefferer > > You try to assign the Array prototype to the prototype of the > jQuery prototype, right? > > After some experiments on the firebug console, I think the > reason for this no working is the Array prototype: It is just > an empty error, see for yourself: > console.debug( Array.prototype ); > Therefore jQuery.fn.extend(Array.prototype) doesn't work either. I'm not sure why it doesn't work, but that's not the reason. Array.prototype is populated with the Array methods, but like most or all of the native objects, those methods are marked as non-enumerable. Try these in the FireBug console: console.debug( Array.prototype.sort ); console.debug( Array.prototype.reverse ); In fact, instead of: jQuery.fn.reverse = [].reverse; This would work the same: jQuery.fn.reverse = Array.prototype.reverse; And you can confirm that they are identical in the FireBug console: [].reverse === Array.prototype.reverse > But we could just add something like this: > var arr = "reverse,pop,push,join,shift,rest of the array > methods".split(","); for(var i=0; i jQuery.fn[arr[i]] = [][arr[i]]; > } That would do the trick. BTW, just for fun, here is another way I like to write these kinds of loops when using a small number of items: var names = { reverse:1, sort:1, etc. }; for( var name in names ) jQuery.fn[name] = [][name]; -Mike ___ jQuery mailing list discuss@jquery.com http://jquery.com/discuss/
Re: [jQuery] .each backwards ?
I agree, do you think .sort() and .reverse() could be added to core jQuery object in the future, or should I just add a plugin. -- View this message in context: http://www.nabble.com/.each-backwards---tf2399145.html#a6694292 Sent from the JQuery mailing list archive at Nabble.com. ___ jQuery mailing list discuss@jquery.com http://jquery.com/discuss/
Re: [jQuery] .each backwards ?
Hi John! > jQuery.fn.unshift = jQuery.fn.unshift; > What is that supposed to do? -- Jörn ___ jQuery mailing list discuss@jquery.com http://jquery.com/discuss/
Re: [jQuery] .each backwards ?
> jQuery.fn.reverse = function() { > this.pushStack(this.get().reverse()); > return this; > } Huh, at first I though that that code would infinitely recurse, I totally forgot that .get() returns a "clean" array of elements - good call! Just a quick simplification: jQuery.fn.reverse = function() { return this.pushStack(this.get().reverse(), arguments); }; going through the other functions, you can easily do .sort() and the other functions too: jQuery.fn.sort = function() { return this.pushStack( [].sort.apply( this, arguments ), []); }; // These don't quite work correctly - but making them work "correctly" would // break jQuery (having duplicate instances of an element) jQuery.fn.push = jQuery.fn.add; jQuery.fn.unshift = jQuery.fn.unshift; jQuery.fn.slice = function() { return this.pushStack( [].slice.apply( this, arguments ), arguments ); }; jQuery.fn.pop = function(fn){ return this.slice( 0, -1, fn ); }; jQuery.fn.shift = function(fn){ return this.slice( 1, this.length, fn ); }; I can definitely see .sort() and .reverse() being useful - along with a better version of slice, but push, pop, shift, unshift are all kind of lame (IMO). --John ___ jQuery mailing list discuss@jquery.com http://jquery.com/discuss/
Re: [jQuery] .each backwards ?
kenton.simpson schrieb: > Thanks for the Idea. This works > > jQuery.fn.reverse = function() { > this.pushStack(this.get().reverse()); > return this; > } > Nice. That is a better approach then just doing jQuery.fn.reverse = [].reverse. -- Jörn ___ jQuery mailing list discuss@jquery.com http://jquery.com/discuss/
Re: [jQuery] .each backwards ?
Thanks for the Idea. This works jQuery.fn.reverse = function() { this.pushStack(this.get().reverse()); return this; } a long that thread a lot more resorting function may be useful. -- View this message in context: http://www.nabble.com/.each-backwards---tf2399145.html#a6693603 Sent from the JQuery mailing list archive at Nabble.com. ___ jQuery mailing list discuss@jquery.com http://jquery.com/discuss/
Re: [jQuery] .each backwards ?
Michael Geary schrieb: >> From: Dossy Shiobara >> >> I'm surprised there's no .reverse(). i.e.: >> >> $(collection).reverse().each(...) >> > > Great idea! > > How about the world's smallest plugin: > >jQuery.fn.reverse = [].reverse; > That's some really great stuff! It took me some time to figure out that you are simply extending jQuery with an existing built-in function... Very neat. > Armed with this knowledge, one might be tempted to load all the Array > methods in one fell swoop: > >jQuery.fn.prototype = Array.prototype; > > But that does not work, presumably because jQuery is already being a bit > sneaky about its Array-like behavior. > You try to assign the Array prototype to the prototype of the jQuery prototype, right? After some experiments on the firebug console, I think the reason for this no working is the Array prototype: It is just an empty error, see for yourself: console.debug( Array.prototype ); Therefore jQuery.fn.extend(Array.prototype) doesn't work either. But we could just add something like this: var arr = "reverse,pop,push,join,shift,rest of the array methods".split(","); for(var i=0; ihttp://jquery.com/discuss/
Re: [jQuery] .each backwards ?
> From: Dossy Shiobara > > I'm surprised there's no .reverse(). i.e.: > > $(collection).reverse().each(...) Great idea! How about the world's smallest plugin: jQuery.fn.reverse = [].reverse; Try it out at http://jquery.com/ by entering these lines into the FireBug console: jQuery.fn.reverse = [].reverse; $('h3').each( function() { console.info( this.innerHTML ); } ); $('h3').reverse().each( function() { console.info( this.innerHTML ); } ); Just for fun, you can add .sort(): jQuery.fn.sort = [].sort; And then try this: $('h3').sort( function( a, b ) { var a = a.innerHTML, b = b.innerHTML; return a > b ? 1 : a < b ? -1 : 0 }).each( function() { console.info( this.innerHTML ); } ); Or all on one line for the FireBug console: $('h3').sort( function( a, b ) { var a = a.innerHTML, b = b.innerHTML; return a > b ? 1 : a < b ? -1 : 0 } ).each( function() { console.info( this.innerHTML ); } ); It looks like you can add pretty much any Array method this way, although most of the others duplicate what you can do in other ways. For example: jQuery.fn.shift = [].shift; And then try: $h3 = $('h3') $h3 $h3.shift() $h3 $h3.shift() $h3 Armed with this knowledge, one might be tempted to load all the Array methods in one fell swoop: jQuery.fn.prototype = Array.prototype; But that does not work, presumably because jQuery is already being a bit sneaky about its Array-like behavior. -Mike ___ jQuery mailing list discuss@jquery.com http://jquery.com/discuss/
Re: [jQuery] .each backwards ?
ff2,0 is up to js1.7 On 10/6/06, Matt Stith <[EMAIL PROTECTED]> wrote: > i know js 1.2 does, but i think its only supported in ff 2.0 > currently... but dont quote me on that. > > On 10/6/06, Ⓙⓐⓚⓔ <[EMAIL PROTECTED]> wrote: > > interesting! > > > > When would length be undefined on an JQ object? > > > > When I first saw the question, I thought of tail recursion, does JS > > deal well (optimize) tail recursion? > > > > On 10/6/06, Blair Mitchelmore <[EMAIL PROTECTED]> wrote: > > > I propose hcae: > > > > > > jQuery.fn.hcae = function( fn, args ) { > > > return jQuery.hcae( this, fn, args ); > > > }; > > > > > > jQuery.hcae = function( obj, fn, args ) { > > > if ( obj.length == undefined ) > > > for ( var i in obj ) > > > fn.apply( obj[i], args || [i, obj[i]] ); > > > else > > > for ( var i = obj.length - 1; i >= 0; --i ) > > > fn.apply( obj[i], args || [i, obj[i]] ); > > > return obj; > > > }; > > > > > > -blair ;) > > > > > > kenton.simpson wrote: > > > > Is there a way to make .each walk backwards threw the element > > collection? > > > > > > ___ > > > jQuery mailing list > > > discuss@jquery.com > > > http://jquery.com/discuss/ > > > > > > > > > -- > > Ⓙⓐⓚⓔ - יעקב ʝǡǩȩ ᎫᎪᏦᎬ > > ___ > > jQuery mailing list > > discuss@jquery.com > > http://jquery.com/discuss/ > > > ___ > jQuery mailing list > discuss@jquery.com > http://jquery.com/discuss/ > -- Ⓙⓐⓚⓔ - יעקב ʝǡǩȩ ᎫᎪᏦᎬ ___ jQuery mailing list discuss@jquery.com http://jquery.com/discuss/
Re: [jQuery] .each backwards ?
On 2006.10.06, Blair Mitchelmore <[EMAIL PROTECTED]> wrote: > I propose hcae: Oh, god no. I see the smiley so I'm guessing you're only kidding, but before someone goes "yeah, that's a good idea ..." > kenton.simpson wrote: > > Is there a way to make .each walk backwards threw the element collection? I'm surprised there's no .reverse(). i.e.: $(collection).reverse().each(...) -- Dossy -- Dossy Shiobara | [EMAIL PROTECTED] | http://dossy.org/ Panoptic Computer Network | http://panoptic.com/ "He realized the fastest way to change is to laugh at your own folly -- then you can let go and quickly move on." (p. 70) ___ jQuery mailing list discuss@jquery.com http://jquery.com/discuss/
Re: [jQuery] .each backwards ?
i know js 1.2 does, but i think its only supported in ff 2.0 currently... but dont quote me on that. On 10/6/06, Ⓙⓐⓚⓔ <[EMAIL PROTECTED]> wrote: > interesting! > > When would length be undefined on an JQ object? > > When I first saw the question, I thought of tail recursion, does JS > deal well (optimize) tail recursion? > > On 10/6/06, Blair Mitchelmore <[EMAIL PROTECTED]> wrote: > > I propose hcae: > > > > jQuery.fn.hcae = function( fn, args ) { > > return jQuery.hcae( this, fn, args ); > > }; > > > > jQuery.hcae = function( obj, fn, args ) { > > if ( obj.length == undefined ) > > for ( var i in obj ) > > fn.apply( obj[i], args || [i, obj[i]] ); > > else > > for ( var i = obj.length - 1; i >= 0; --i ) > > fn.apply( obj[i], args || [i, obj[i]] ); > > return obj; > > }; > > > > -blair ;) > > > > kenton.simpson wrote: > > > Is there a way to make .each walk backwards threw the element > collection? > > > > ___ > > jQuery mailing list > > discuss@jquery.com > > http://jquery.com/discuss/ > > > > > -- > Ⓙⓐⓚⓔ - יעקב ʝǡǩȩ ᎫᎪᏦᎬ > ___ > jQuery mailing list > discuss@jquery.com > http://jquery.com/discuss/ > ___ jQuery mailing list discuss@jquery.com http://jquery.com/discuss/
Re: [jQuery] .each backwards ?
interesting! When would length be undefined on an JQ object? When I first saw the question, I thought of tail recursion, does JS deal well (optimize) tail recursion? On 10/6/06, Blair Mitchelmore <[EMAIL PROTECTED]> wrote: > I propose hcae: > > jQuery.fn.hcae = function( fn, args ) { > return jQuery.hcae( this, fn, args ); > }; > > jQuery.hcae = function( obj, fn, args ) { > if ( obj.length == undefined ) > for ( var i in obj ) > fn.apply( obj[i], args || [i, obj[i]] ); > else > for ( var i = obj.length - 1; i >= 0; --i ) > fn.apply( obj[i], args || [i, obj[i]] ); > return obj; > }; > > -blair ;) > > kenton.simpson wrote: > > Is there a way to make .each walk backwards threw the element collection? > > ___ > jQuery mailing list > discuss@jquery.com > http://jquery.com/discuss/ > -- Ⓙⓐⓚⓔ - יעקב ʝǡǩȩ ᎫᎪᏦᎬ ___ jQuery mailing list discuss@jquery.com http://jquery.com/discuss/
Re: [jQuery] .each backwards ?
I propose hcae: jQuery.fn.hcae = function( fn, args ) { return jQuery.hcae( this, fn, args ); }; jQuery.hcae = function( obj, fn, args ) { if ( obj.length == undefined ) for ( var i in obj ) fn.apply( obj[i], args || [i, obj[i]] ); else for ( var i = obj.length - 1; i >= 0; --i ) fn.apply( obj[i], args || [i, obj[i]] ); return obj; }; -blair ;) kenton.simpson wrote: > Is there a way to make .each walk backwards threw the element collection? ___ jQuery mailing list discuss@jquery.com http://jquery.com/discuss/