Re: JS - Recursion and Closures

kangax
Tue, 27 May 2008 19:01:13 -0000

Moreover, #each (and most of enumerable methods) accepts "context"
object as a second argument - since this is such a common need.

SomeComplexDataStructure.DataPart.each( function(DataPart) {
  this.myRecursiveFunction(DataPart);
}, this );

It''s also good to remember that using bind is a little slower than
storing context in a separate variable.

- kangax


On May 27, 2:44 pm, "Ryan Gahl"
<ryan.g...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
wrote:
> :)
>
> Both are good patterns to know about (mine of course only works if you are
> using prototype while yours is good old javascript).
>
> On 5/27/08, Frederick Polgardy
<f...-SMQUYeM9IBBWk0Htik3J/w@public.gmane.org> wrote:
>
>
>
>
>
> > Buy him a beer instead; this solution is a lot better than mine in
this
> > case.
>
> > On Tue, May 27, 2008 at 1:04 PM, Ryan Gahl
<ryan.g...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:
>
> >> Another way to do this could be to just use proto''s
.bind(), assuming the
> >> "myRecursiveFunction" is only ever called as an instance
method (vs. calling
> >> it statically against the MyNS.Package''s prototype)...
>
> >> So:
>
> >> SomeComplexDataStructure.DataPart.each( function(DataPart) {
> >> *this.myRecursiveFunction(DataPart);* // or whatever the args
should be
> >> }.bind(this) );
>
> > --
> > Science answers questions; philosophy questions answers.
>
> --
> Ryan Gahl
> Manager, Senior Software Engineer
> Nth Penguin, LLChttp://www.nthpenguin.com
> --
> WebWidgetry.com / MashupStudio.com
> Future Home of the World''s First Complete Web Platform
> --
> Inquire: 1-920-574-2218
> Blog:http://www.someElement.com
> LinkedIn Profile:http://www.linkedin.com/in/ryangahl