Re: JS - Recursion and Closures

Ryan Gahl
Tue, 27 May 2008 18:04:11 -0000

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) );

On 5/27/08, Frederick Polgardy
<fred-SMQUYeM9IBBWk0Htik3J/w@public.gmane.org>
wrote:
>
> I''ll never refuse a free beer. ;-)
>
> On the example code, there are two other things to keep in mind:
>
> 1) Have to save away the value of arguments.callee before going into the
> inner function (arguments is always bound to the innermost function);
> 2) If you want to actually call the function recursively *as a method of
> this*, the above syntax won''t work.  You''ll need to do
something like:
>
> MyNS.Package.prototype.myRecursiveFunction >
function(SomeComplexDataStructure) {
> *    var outer_fn = arguments.callee;
>     var outer_this = this;
> *
>     SomeComplexDataStructure.DataPart.each( function(DataPart) {
> *      outer_fn.call(outer_this, DataPart);* // or whatever the args
> should be
>     } );
>   }
>
> On Mon, May 26, 2008 at 2:48 PM, Jonathan Logue
<jonathanlogue-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
> wrote:
>
>>
>> Thanks so much! I kept reading about how the inner function had access
>> to the members of the outer function but could not find an example of
>> calling the actual function. You are a life saver.
>>
>> I''ll buy you a beer if I am ever in Chicago!
>>
>> -Jonathan
>
>
> --
> Science answers questions; philosophy questions answers.
> >
>


--
Ryan Gahl
Manager, Senior Software Engineer
Nth Penguin, LLC
http://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