Hi Derek, Nice point, I didn't know that. Could you provide an alternate version of the "class" using that approach? And do you know if that would work on IE6?
Leonardo Braga @LeonardoBraga On Dec 13, 5:08 pm, Derek Gathright <[email protected]> wrote: > Iterators were introduce in JS1.5. Look into the yield expression, it's > pretty useful for looping over stuff on-demand (as opposed to all at once). > > https://developer.mozilla.org/en/Core_JavaScript_1.5_Guide/Iterators_... > > On Mon, Dec 13, 2010 at 10:53 AM, Leonardo Braga > <[email protected]>wrote: > > > > > > > > > Hi, > > > For a gallery I was writing, I needed to traverse an array in a loop > > way, allowing me to move it forward or backward to create a carousel > > effect on an image list, so I wrote this tiny "class" for that. As you > > can see I didn't write any validations. You are free to fix, extend, > > derivate or copy and I would appreciate if you post any modifications > > here. > > > /*--------------------------*/ > > > function Loopr(_arr) { > > this.array = _arr; > > this.position = -1; > > > this.next = function() { > > (!(++this.position < this.array.length)) && (this.position = > > 0); > > return this.array[this.position]; > > } > > > this.prev = function() { > > (!(--this.position > -1)) && (this.position = > > this.array.length - > > 1); > > return this.array[this.position]; > > } > > } > > > // Tests > > var test = ['a', 'b', 'c'], loopr, resultTests; > > > loopr = new Loopr(test); > > > resultTests = []; > > resultTests.push(loopr.next()); > > resultTests.push(loopr.next()); > > resultTests.push(loopr.next()); > > resultTests.push(loopr.next()); > > resultTests.push(loopr.next()); > > resultTests.push(loopr.next()); > > > // result: 'a', 'b', 'c', 'a', 'b', 'c' > > > loopr = new Loopr(test); > > > resultTests = []; > > resultTests.push(loopr.prev()); > > resultTests.push(loopr.prev()); > > resultTests.push(loopr.prev()); > > resultTests.push(loopr.prev()); > > resultTests.push(loopr.prev()); > > resultTests.push(loopr.prev()); > > > // result: 'c', 'b', 'a', 'c', 'b', 'a' > > > /*--------------------------*/ > > > I hope it helps someone. > > > Leonardo Braga > > @LeonardoBraga > > > -- > > You received this message because you are subscribed to the Google Groups > > "The JSMentors JavaScript Discussion Group" group. > > To post to this group, send email to [email protected]. > > To unsubscribe from this group, send email to > > [email protected]<jsmentors%2bunsubscr...@googlegroups > > .com> > > . > > For more options, visit this group at > >http://groups.google.com/group/jsmentors?hl=en. -- You received this message because you are subscribed to the Google Groups "The JSMentors JavaScript Discussion Group" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/jsmentors?hl=en.
