Re: JS - Recursion and Closures

Frederick Polgardy
Tue, 27 May 2008 17:01:02 -0000

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.